Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put compiler command line args in specific places using cmake

Tags:

g++

gnu

cmake

ld

So I'm building a shared library, out of two static libraries.

This answer says the way to do it is to insert -Wl,--whole-archive before my static libs, the -Wl,--no-whole-archive after them.

So what I have in cmake at the moment for the shared library is:

add_library(wittyPlus SHARED empty.cpp)
target_link_libraries(wittyPlus 
    ${wtdbosqlite}
    ${WT_LIBRARIES}
    ${DB_LIBRARIES}
    ${Boost_LIBRARIES}
    app models
)

So what I need is for it to add the -Wl,--whole-archive before app and models, then -Wl,--no-whole-archive after them (so that the standard library imports don't get exported by the shared lib).

What's the easiest way to do this in CMake ?


Addition: So I'd like to use the standard cmake stuff as much as possible, that way I don't have to do any extra work for windows builds, as CMake kindly removes the compiler definitions that aren't supported on the platform being built.

like image 394
matiu Avatar asked Jan 15 '13 21:01

matiu


People also ask

How use CMake command line?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

Is CMake a C++ compiler?

Installation. C++ CMake tools for Windows is installed as part of the Desktop development with C++ and Linux Development with C++ workloads. Both C++ CMake tools for Windows and Linux Development with C++ are required for cross-platform CMake development.

What is CMake build?

CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists. txt. Users build a project by using CMake to generate a build system for a native tool on their platform.


1 Answers

OK, it was much easier than I thought.

So according to the cmake docs:

Item names starting with '-', but not '-l' or '-framework', are treated as linker flags.

So the fix was just to:

add_library(wittyPlus SHARED empty.cpp)
target_link_libraries(wittyPlus 
    ${wtdbosqlite}
    ${WT_LIBRARIES}
    ${DB_LIBRARIES}
    ${Boost_LIBRARIES}
    "-Wl,--whole-archive"
    app models
    "-Wl,--no-whole-archive"
)

I don't know if it'll work on windows or not, I expect it will as cmake is super clever.

Looking at the result with objdump, it does seem to have a lot of boost stuff in the exports, so I might be doing something wrong.

objdump -C -t wittyPlus/libwittyPlus.so | grep -i boost

But it does have the stuff I need to link against it so, that's a step forward.

Any other answers still appreciated. Basically what I'm trying to do is the same as this question:

CMake: how create a single shared library from all static libraries of subprojects?

like image 84
matiu Avatar answered Oct 02 '22 20:10

matiu