In my example code:
main.cpp
#include <iostream>
#if defined(_WIN32) || defined(__WIN32__)
#define EXPORT extern "C" __declspec(dllexport)
#elif defined(linux) || defined(__linux)
#define EXPORT __attribute__((visibility("default")))
#endif
EXPORT void hello()
{
std::cout<<"Hello world!"<<std::endl;
}
/* not exported */ void goodbye()
{
std::cout<<"Goodbye!"<<std::endl;
}
compiled via options as followed:
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(my_test)
add_library(output SHARED
main.cpp
)
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s -Wall -Wextra -std=c++17 -fvisibility=hidden -fvisibility-inlines-hidden")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -ffunction-sections")
#set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections -Wl,--strip-all")
I see symbols rather than the hello function:
nm -D liboutput.so
The result:
U __cxa_atexit
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000001179 T _Z5hellov
U _ZNSolsEPFRSoS_E
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
I do not want the functions such as _ZSt4cout be visible from outside. My problem is not solved even if I enable the last two lines of the cmake recipe.
How can I achieve that?