[CMake] Help needed with '-whole-archive,-export-dynamic'

David Aldrich david.aldrich.ntml at gmail.com
Tue Oct 22 08:32:31 EDT 2019


Hi

I am porting a gnu make project to CMake. Initially I am using the Ninja
generator and running in Ubuntu 18.04.

The project consists of a static library 'libKernel.a', which includes
main.cpp,
which we link into an executable: 'MyApp'. The program dynamically loads
shared
libraries that need objects from 'libKernel.a' so we use options such as
-whole-archive and -export-dynamic.

The original link command for the program is:

g++ -o _gnuRelease/MyApp -Wl,-whole-archive,-export-dynamic,--no-as-needed
../Kernel/_gnuRelease/libKernel.a -Wl,--as-needed,--no-whole-archive
-lpthread -ldl

In CMake I have implemented this as:

add_executable(MyApp ../Kernel/main.cpp)
set_target_properties(MyApp PROPERTIES ENABLE_EXPORTS TRUE)
target_link_libraries(MyApp Kernel ${CMAKE_DL_LIBS})

and CMake's link command is:

/usr/bin/c++  -O3 -DNDEBUG  -Wl,--export-dynamic -rdynamic
CMakeFiles/MyApp.dir/Kernel/main.cpp.o  -o release/MyApp
 Kernel/libKernel.a -ldl

The CMake build is failing to link at runtime as some symbols are missing.

I have two issues here:

1) In the CMake implementation I removed main.cpp from libKernel and
specified
it as the executable source file, as it seems that the executable needs at
least one SOURCE file.  Is there a way around this?

2) How can I tell CMake to use the
'-Wl,-whole-archive,-export-dynamic,--no-as-needed' flags?

Best regards

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191022/7568b21d/attachment.html>


More information about the CMake mailing list