Skip to content

Instantly share code, notes, and snippets.

@jpret
Last active June 28, 2021 09:11
Show Gist options
  • Select an option

  • Save jpret/83b1f59082205ad7a582dda3d59d67d5 to your computer and use it in GitHub Desktop.

Select an option

Save jpret/83b1f59082205ad7a582dda3d59d67d5 to your computer and use it in GitHub Desktop.
Create static member in case of several shared libraries
#!/usr/bin/env bash
# clean dir if exists
rm -rf build
mkdir build
cd build || exit
CXX=clang++ CC=clang cmake ..
make
#!/usr/bin/env bash
# clean dir if exists
rm -rf build
mkdir build
cd build || exit
CXX=g++ CC=gcc cmake ..
make
cmake_minimum_required(VERSION 2.8)
project(foo)
# Change the default to hidden, then you need to explicitly set it
# Thus, normally, you shouldn't have the problem
add_compile_options(-fvisibility=hidden) # only for testing the EXPORT define
# Create the shared library
add_library(mylib SHARED mylib.cpp)
# Create EXPORT header
include(GenerateExportHeader)
generate_export_header(mylib)
include_directories(${CMAKE_BINARY_DIR})
add_executable(example main.cpp foo.cpp) # foo also used mylib
# Link the lib
target_link_libraries(example mylib)
#include "foo.h"
#include <iostream>
#include "mylib.h"
void foo () {
std::cout << "foo -> A<int>: " << A<int>::x << std::endl;
std::cout << "foo -> A<bool>: " << A<bool>::x << std::endl;
}
#pragma once
void foo();
#include <iostream>
#include "foo.h"
#include "mylib.h"
int main () {
std::cout << "main -> A<int>: " << A<int>::x << std::endl;
std::cout << "main -> A<bool>: " << A<bool>::x << std::endl;
// Making changes
A<int>::x = 2;
A<bool>::x = true;
// Calling from foo
foo();
return 0;
}
#include "mylib.h"
template<> int A<int>::x = 1;
template<> int A<bool>::x = false;
#pragma once
#include <mylib_export.h>
template<typename T>
struct A {
MYLIB_EXPORT static int x;
};
#ifndef MYLIB_EXPORT_H
#define MYLIB_EXPORT_H
#ifdef MYLIB_STATIC_DEFINE
# define MYLIB_EXPORT
# define MYLIB_NO_EXPORT
#else
# ifndef MYLIB_EXPORT
# ifdef mylib_EXPORTS
/* We are building this library */
# define MYLIB_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define MYLIB_EXPORT __attribute__((visibility("default")))
# endif
# endif
# ifndef MYLIB_NO_EXPORT
# define MYLIB_NO_EXPORT __attribute__((visibility("hidden")))
# endif
#endif
#ifndef MYLIB_DEPRECATED
# define MYLIB_DEPRECATED __attribute__ ((__deprecated__))
# define MYLIB_DEPRECATED_EXPORT MYLIB_EXPORT __attribute__ ((__deprecated__))
# define MYLIB_DEPRECATED_NO_EXPORT MYLIB_NO_EXPORT __attribute__ ((__deprecated__))
#endif
#define DEFINE_NO_DEPRECATED 0
#if DEFINE_NO_DEPRECATED
# define MYLIB_NO_DEPRECATED
#endif
#endif
#ifndef MYLIB_EXPORT_H
#define MYLIB_EXPORT_H
#ifdef MYLIB_STATIC_DEFINE
# define MYLIB_EXPORT
# define MYLIB_NO_EXPORT
#else
# ifndef MYLIB_EXPORT
# ifdef mylib_EXPORTS
/* We are building this library */
# define MYLIB_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define MYLIB_EXPORT __attribute__((visibility("default")))
# endif
# endif
# ifndef MYLIB_NO_EXPORT
# define MYLIB_NO_EXPORT __attribute__((visibility("hidden")))
# endif
#endif
#ifndef MYLIB_DEPRECATED
# define MYLIB_DEPRECATED __attribute__ ((__deprecated__))
#endif
#ifndef MYLIB_DEPRECATED_EXPORT
# define MYLIB_DEPRECATED_EXPORT MYLIB_EXPORT MYLIB_DEPRECATED
#endif
#ifndef MYLIB_DEPRECATED_NO_EXPORT
# define MYLIB_DEPRECATED_NO_EXPORT MYLIB_NO_EXPORT MYLIB_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef MYLIB_NO_DEPRECATED
# define MYLIB_NO_DEPRECATED
# endif
#endif
#endif /* MYLIB_EXPORT_H */
#ifndef MYLIB_EXPORT_H
#define MYLIB_EXPORT_H
#ifdef MYLIB_STATIC_DEFINE
# define MYLIB_EXPORT
# define MYLIB_NO_EXPORT
#else
# ifndef MYLIB_EXPORT
# ifdef mylib_EXPORTS
/* We are building this library */
# define MYLIB_EXPORT __declspec(dllexport)
# else
/* We are using this library */
# define MYLIB_EXPORT __declspec(dllimport)
# endif
# endif
# ifndef MYLIB_NO_EXPORT
# define MYLIB_NO_EXPORT
# endif
#endif
#ifndef MYLIB_DEPRECATED
# define MYLIB_DEPRECATED __declspec(deprecated)
#endif
#ifndef MYLIB_DEPRECATED_EXPORT
# define MYLIB_DEPRECATED_EXPORT MYLIB_EXPORT MYLIB_DEPRECATED
#endif
#ifndef MYLIB_DEPRECATED_NO_EXPORT
# define MYLIB_DEPRECATED_NO_EXPORT MYLIB_NO_EXPORT MYLIB_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef MYLIB_NO_DEPRECATED
# define MYLIB_NO_DEPRECATED
# endif
#endif
#endif /* MYLIB_EXPORT_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment