[CMake] Linking problem

Juan Sanchez Juan.Sanchez at amd.com
Wed Nov 7 11:51:28 EST 2007


I believe this to be the more accurate than the FAQ and my previous
example.  In addition, the static and shared libraries can have the same
name.

Note that cat.cc is an empty file, since the linker will not work
without at least one object file on the dynamic target.

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_COMPILER_IS_GNUCC)
   ADD_LIBRARY(zzSTATIC STATIC dog.cc)
   ADD_LIBRARY(zzDYNAMIC SHARED cat.cc)
   TARGET_LINK_LIBRARIES(zzDYNAMIC -Wl,-whole-archive zzSTATIC
-Wl,-no-whole-archive)
   SET_TARGET_PROPERTIES(
     zzSTATIC PROPERTIES
     OUTPUT_NAME zz
     CLEAN_DIRECT_OUTPUT 1
     COMPILE_FLAGS -fPIC
   )
   SET_TARGET_PROPERTIES(
     zzDYNAMIC PROPERTIES
     OUTPUT_NAME zz
     CLEAN_DIRECT_OUTPUT 1
   )
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_COMPILER_IS_GNUCC)

Regards,

Juan

Juan Sanchez wrote:
> Hello,
> 
> There a much nicer platform independent writeup on this at:
> 
> http://www.cmake.org/Wiki/CMake_FAQ#Does_that_mean_I_have_to_build_all_my_library_objects_twice.2C_once_for_shared_and_once_for_static.3F.21__I_don.27t_like_that.21
> 
> There may be a couple of syntactical issues with the writeup.
> 
> For example, you may need to have at least one source file composing the
> shared library, in addition to the static libraries that you want to ad.
> 
> The "-fPIC" should be set in SET_TARGET_PROPERTIES and not in
> ADD_LIBRARY.  I believe the example I have below to be correct, but your
> mileage may vary.
> 
> Regards,
> 
> Juan
> 
> Juan Sanchez wrote:
>> Two things with convenience libraries.  Part of this may be in the FAQ:
>>
>> On linux, compile the code fPIC or fpic (slightly different meanings).
>>
>> So for library hello:
>> SET_TARGET_PROPERTIES(
>> hello PROPERTIES
>> OUTPUT_NAME hello
>> CLEAN_DIRECT_OUTPUT 1
>> COMPILE_FLAGS -fPIC
>> )
>>
>> where hello is the name of your static convenience library.
>>
>> Make sure all symbols get put into the so:
>> target_link_libraries(mywrap -Wl,-whole-archive
>> hello -Wl,-no-whole-archive)
>>
>> where mywrap is the name of the shared library, and hello is the static
>> library you want attached to it.  If you are attaching multiple
>> libraries, linker order may be important.
>>
>> Juan
>>
>>
>>
>> Salvatore Iovene wrote:
>>> Hi,
>>> I'm not 100% sure this is really a CMake related question, but I'll
>>> fire it up anyway:
>>>
>>> I'm building a series of static libraries, name them liba.a, libb.a
>>> and libc.a, and linking them into a shared library libfoo.so.
>>>
>>> Then I'm building libx.a liby.a and libz.a and linking them into the
>>> shared libbar.so.
>>>
>>> Then I have an executable whatever.exe that's linked to to libfoo.so
>>> and libbar.so. The linking of the executable fails complaining of
>>> certain missing simbols. Some symbols from liba.a are missing in
>>> libbar.so.
>>>
>>> So far I have "fixed" the issue by linking one of the libs of
>>> libfoo.so (say libx.a) to libbar.so. But this smells of nasy
>>> workaround.
>>>
>>> Obviously I'm doing something wrong here, so could anyone please help?
>>>
>>> Thanks!
>>>
>>
> 
> 


-- 
Juan Sanchez
Juan.Sanchez at amd.com
800-538-8450 Ext. 54395
512-602-4395




More information about the CMake mailing list