[CMake] SHARED library containing OBJECT library: Missing -fPIC

Leif Walsh leif.walsh at gmail.com
Fri Jun 22 09:55:55 EDT 2012


I tried this in my project. I added -fPIC to the COMPILE_FLAGS property of the object library and it worked, but then you also get PIC static libraries (which isn't that big of a deal). But time your compiles. Usually the compilation of individual c files is well dominated by the linking time, especially with optimization. So to reduce technical risk, I opted just to build each library the old fashioned way and compile objects twice. I saw pretty much no compile time difference. But maybe you have other reasons. 

Sent from my iPhone

On Jun 22, 2012, at 5:46, Andreas Naumann <Andreas-Naumann at gmx.net> wrote:

> I think the latter is the case. It should not be allowed to compose a shared library from OBJECT libraries.
> What does the cmake developer think about this problem?
> 
> Regards,
> Andreas
> 
> Am 22.06.2012 11:14, schrieb Benjamin Eikel:
>> Hello Andreas,
>> 
>> Am Freitag, 22. Juni 2012, 11:09:36 schrieb Andreas Naumann:
>>   
>>> Hello Benjamin,
>>> 
>>> if you wants to use an object file for a shared library, this object
>>> file has to be compiled with -fPIC. I don't think, that it is possible
>>> to create a shared library from such object files.
>>>     
>> I know that this is the case. My question is not directed to -fPIC in general,
>> but to CMake's handling of these files. I want to use CMake's new OBJECT
>> library feature to structure the build of a library. The library is built
>> statically as well as dynamically. When building statically, there is no
>> problem in using the OBJECT libraries of CMake. But when building dynamically,
>> the problem described in my original e-mail arises. CMake's documentation does
>> not say that it is not allowed to use OBJECT libraries to compose shared
>> libraries.
>> 
>> Kind regards
>> Benjamin
>> 
>>   
>>> Regards,
>>> Andreas
>>> 
>>> Am 22.06.2012 09:50, schrieb Benjamin Eikel:
>>>     
>>>> Hello,
>>>> 
>>>> I have a problem using an OBJECT library that I want to compile into a
>>>> SHARED library using CMake version 2.8.8.
>>>> 
>>>> Here is a small example that demonstrates my problem:
>>>> 
>>>> # --------------- CMakeLists.txt ---------------
>>>> cmake_minimum_required(VERSION 2.8.8)
>>>> project(CMakeTest CXX)
>>>> add_library(MyLibSub OBJECT
>>>> 
>>>>          ClassA.cpp
>>>> 
>>>> )
>>>> add_library(MyLib SHARED
>>>> 
>>>>          $<TARGET_OBJECTS:MyLibSub>
>>>>          ClassB.cpp
>>>> 
>>>> )
>>>> 
>>>> The content of the other four files is more or less irrelevant. To make
>>>> the example complete, I added them at the end of this e-mail.
>>>> 
>>>> When I want to build this example, I get the following error:
>>>> 
>>>> $ mkdir build&&   cd build&&   cmake ..&&   make
>>>> -- The CXX compiler identification is GNU 4.7.0
>>>> -- Check for working CXX compiler: /usr/bin/c++
>>>> -- Check for working CXX compiler: /usr/bin/c++ -- works
>>>> -- Detecting CXX compiler ABI info
>>>> -- Detecting CXX compiler ABI info - done
>>>> -- Configuring done
>>>> -- Generating done
>>>> -- Build files have been written to: /home/benjamin/Desktop/CMake
>>>> test/build Scanning dependencies of target MyLibSub
>>>> [ 50%] Building CXX object CMakeFiles/MyLibSub.dir/ClassA.cpp.o
>>>> [ 50%] Built target MyLibSub
>>>> Scanning dependencies of target MyLib
>>>> [100%] Building CXX object CMakeFiles/MyLib.dir/ClassB.cpp.o
>>>> Linking CXX shared library libMyLib.so
>>>> /usr/bin/ld: CMakeFiles/MyLibSub.dir/ClassA.cpp.o: relocation R_X86_64_32
>>>> against `.rodata' can not be used when making a shared object; recompile
>>>> with -fPIC
>>>> CMakeFiles/MyLibSub.dir/ClassA.cpp.o: could not read symbols: Bad value
>>>> collect2: error: ld returned 1 exit status
>>>> make[2]: *** [libMyLib.so] Error 1
>>>> make[1]: *** [CMakeFiles/MyLib.dir/all] Error 2
>>>> make: *** [all] Error 2
>>>> 
>>>> When I add the line
>>>> set_target_properties(MyLibSub PROPERTIES COMPILE_FLAGS "-fPIC")
>>>> to the CMakeLists.txt, everything works fine. Am I doing something wrong?
>>>> Should CMake add "-fPIC" automatically in this case? Your feedback is
>>>> greatly appreciated.
>>>> 
>>>> Kind regards
>>>> Benjamin
>>>> 
>>>> 
>>>> 
>>>> // --------------- ClassA.cpp ---------------
>>>> #include "ClassA.h"
>>>> #include<iostream>
>>>> 
>>>> void ClassA::printName() {
>>>> 
>>>>          std::cout<<   "ClassA"<<   std::endl;
>>>> 
>>>> }
>>>> // --------------- ClassA.h ---------------
>>>> #ifndef CLASSA_H
>>>> #define CLASSA_H
>>>> 
>>>> struct ClassA {
>>>> 
>>>>          void printName();
>>>> 
>>>> };
>>>> 
>>>> #endif /* CLASSA_H */
>>>> // --------------- ClassB.cpp ---------------
>>>> #include "ClassB.h"
>>>> #include<iostream>
>>>> 
>>>> void ClassB::printName() {
>>>> 
>>>>          std::cout<<   "ClassB"<<   std::endl;
>>>>          a.printName();
>>>> 
>>>> }
>>>> // --------------- ClassB.h ---------------
>>>> #ifndef CLASSB_H
>>>> #define CLASSB_H
>>>> 
>>>> #include "ClassA.h"
>>>> 
>>>> struct ClassB {
>>>> 
>>>>          void printName();
>>>>          ClassA a;
>>>> 
>>>> };
>>>> 
>>>> #endif /* CLASSB_H */
>>>> 
>>>> --
>>>> 
>>>> Powered by www.kitware.com
>>>> 
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>> 
>>>> Please keep messages on-topic and check the CMake FAQ at:
>>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>> 
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.cmake.org/mailman/listinfo/cmake
>>>>       
>>> --
>>> 
>>> Powered by www.kitware.com
>>> 
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>> 
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>> 
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.cmake.org/mailman/listinfo/cmake
>>>     
>> --
>> 
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>> 
>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>> 
>>   
> 
> --
> 
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list