[CMake] Clarification around "target_link_libraries"

SF Markus Elfring elfring at users.sourceforge.net
Fri Sep 13 03:49:29 EDT 2013


>> Now I try to give a build configurator the choice with which library type
>> some executable files should be linked. It seems that I need to construct
>> corresponding file names instead of target names.
> 
> No, you don't have to.

Thanks for your feedback.


> Do you want to build static and dynamic lib at the same time ?

Yes.


> Then do something like this ?

My CMake scripts used an approach that was similar to your description. Now I
wonder which of my previous typos let me think that the desired target name
could not be applied there.


> add_executable(hello ${hellosSrcs})
> 
> target_link_libraries(hello ${myLibs} )

Documentation:
"...
If a library name matches that of another target in the project a dependency
will automatically be added in the build system to make sure the library being
linked is up-to-date before the target links.
..."


I have got still two open issues (on my side?).

1. The target name can be resolved inside the build system. But how should I
determine generated file names when I want to refer to them from external
sources like installation directories?

2. I stumble on the following situation with the tools "CMake 2.8.11.2" and "GCC
4.7.2 20130108" on my openSUSE system.

...
make -f examples++/CMakeFiles/client_cpp.dir/build.make
examples++/CMakeFiles/client_cpp.dir/build
make[2]: Entering directory `/home/elfring/Projekte/libsocket/Bau'
Linking CXX executable client
cd /home/elfring/Projekte/libsocket/Bau/examples++ && /usr/bin/cmake -E
cmake_link_script CMakeFiles/client_cpp.dir/link.txt --verbose=1
/usr/bin/c++   -O2 -g -DNDEBUG    CMakeFiles/client_cpp.dir/client.cpp.o  -o
client -rdynamic ../libsocket_cpp.a ../libisocket.a ../libusocket.a
../libsocket_cpp.a(inetclientstream.cpp.o): In function
`libsocket::inet_stream::connect(char const*, char const*, int, int)':
/home/elfring/Projekte/libsocket/lokal/C++/inetclientstream.cpp:101: undefined
reference to `create_inet_stream_socket(char const*, char const*, char, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [examples++/client] Error 1
make[2]: Leaving directory `/home/elfring/Projekte/libsocket/Bau'
...

I find that strange because the library file which contains the "missed
function" is usable for the C program examples as the software generation and a
corresponding small test run acknowledges.

Regards,
Markus


More information about the CMake mailing list