[CMake] Problem with creating shared library

Sebastián Mancilla smancill at jlab.org
Tue Aug 21 18:51:05 EDT 2018


I fixed your problem. Get the attached patch and apply it with "git apply
<patch_file>".

The kImageAnnotator constructor was private to the library (I just learned
that Qt does that when creating shared libraries), and that's why you get
the undefined reference error. You could have checked it with:

nm lib/libkImageAnnotator.so | c++filt | grep ::KImage
00000000000141f6 t KImageAnnotator::KImageAnnotator(QPixmap const&)
00000000000141f6 t KImageAnnotator::KImageAnnotator(QPixmap const&)

The "t" shows it is private.

You have to set the proper export macros to make it visible:

https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application
http://doc.qt.io/qt-5/sharedlibrary.html

The patch does that, and now the example links with the library just fine.
The kImageAnnotatorExport.h header defines the macro, the compile
definition is set for the shared library target in the CMakeLists.txt. I
guess you'll  have to do the same for all classes used by the unit tests,
no sure of the Qt development practices.

I also reworked your CMake files a bit. I am sending a single patch,
though. Sorry.


El mar., 21 de ago. de 2018 a la(s) 14:29, Eric Noulard (
eric.noulard at gmail.com) escribió:

>
>
> Le lun. 20 août 2018 à 19:05, Damir Porobic <damir_porobic at live.com> a
> écrit :
>
>> Hi Eric,
>>
>>
>> yes, this is the project. I have pushed my current state to this branch
>> https://github.com/DamirPorobic/kImageAnnotator/tree/sharedLibTestBranch
>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_DamirPorobic_kImageAnnotator_tree_sharedLibTestBranch&d=DwMFaQ&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=r4LA5jjr_jrRNhddqyy5IsuqRRwHEPCwRMFbxoU1ChI&s=j_vYMAeNlkSJPzPWt5eGGRXiqm4RocWUGgyctA8SGE4&e=>
>>
>> I've tried also without the generate_export_headers (cleaned everything
>> up before trying out) but I get the same result.
>>
>>
>> KImageAnnotator::KImageAnnotator(QPixmap const&)should be exposed by the
>> handwritten file, that's true, I think I got something mixed up there and I
>> don't actually need the generate_export_headers, but as said, even
>> without the line, it's not working.
>>
>
> I'm quite lost with the file layout.
>
> in example/main.cpp you do:
> #include <kImageAnnotator/KImageAnnotator.h>
> so I guess you expect that
> target_link_libraries(kImageAnnotator-example PRIVATE
> kImageAnnotator::kImageAnnotator)
>
> will bring you the include path to "kImageAnnotator/KImageAnnotator.h"
> along with the [imported] target
>
> in your main tree (not in example) there is a trick because you did:
>
> add_library(kImageAnnotator::kImageAnnotator ALIAS kImageAnnotator)
>
> and then in the test directory you do:
>
> target_link_libraries(${UnitTestName} Qt5::Test kimageannotator_LIB)
>
> with
>
> add_library(kimageannotator_LIB STATIC ${kimageannotator_SRCS})
>
> so AFAIU you compile your kImageAnnotator library twice. Once for building
> the target you expect to use in example/ subdir
> and another time as a STATIC lib for the unit test. So the unit test is
> not linking to the same object lib at all ??
>
> I am not used to KDE development but all this seems very fuzzy to me.
>
> May be you could get more precise help from people who know better about
> KDE dev and the specific CMake machinery that comes along
> like ECM (https://github.com/KDE/extra-cmake-modules
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_KDE_extra-2Dcmake-2Dmodules&d=DwMFaQ&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=r4LA5jjr_jrRNhddqyy5IsuqRRwHEPCwRMFbxoU1ChI&s=LaNKLek4ewlsoy7ECnW4qRmkBAAVUeo6iTSFqsT9ACo&e=>)
> you seems to be using.
>
> --
> Eric
>


-- 
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180821/5e64f078/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kia.patch
Type: application/octet-stream
Size: 18882 bytes
Desc: not available
URL: <https://cmake.org/pipermail/cmake/attachments/20180821/5e64f078/attachment-0001.obj>


More information about the CMake mailing list