[CMake] Problem with creating shared library

Damir Porobic damir_porobic at live.com
Sun Aug 26 07:50:07 EDT 2018


Ok, I've found the issue for the latest problem but I'm not sure how to fix it in the CMake file. The make was using an old version of /usr/local/lib64/libkImageAnnotator.so.0.0.1 so I removed everything with kImageAnnotato in its name from this directory and run again the sudo make install which gave me this output


-- Install configuration: "Debug"
-- Up-to-date: /usr/local/lib64/cmake/kImageAnnotator/kImageAnnotator-config.cmake
-- Up-to-date: /usr/local/lib64/cmake/kImageAnnotator/kImageAnnotator-config-version.cmake
-- Up-to-date: /usr/local/lib64/cmake/kImageAnnotator/kImageAnnotator-targets.cmake
-- Up-to-date: /usr/local/lib64/cmake/kImageAnnotator/kImageAnnotator-targets-debug.cmake
-- Installing: /usr/local/lib64/libkImageAnnotator.so
-- Set runtime path of "/usr/local/lib64/libkImageAnnotator.so" to "/usr/local/lib64"
-- Up-to-date: /usr/local/include/kImageAnnotator
-- Up-to-date: /usr/local/include/kImageAnnotator/KImageAnnotator.h
-- Up-to-date: /usr/local/include/kImageAnnotator/KImageAnnotatorExport.h


So only libkImageAnnotator.so is created and installed, but not the one with the version at the end which cmake seems to expect: in my test project

CMake Error at /usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake:79 (message):
  The imported target "kImageAnnotator" references the file

     "/usr/local/lib64/libkImageAnnotator.so.0.0.1"

  but this file does not exist.

When I manually create a link to the installed file:

$ sudo ln -s /usr/local/lib64/libkImageAnnotator.so /usr/local/lib64/libkImageAnnotator.so.0.0.1

Everything finally works, but I need to fix this final issue with the missing link. This was working earlier I think.


________________________________
From: Damir Porobic <damir_porobic at live.com>
Sent: Friday, August 24, 2018 9:37:30 PM
To: Sebastián Mancilla
Cc: cmake at cmake.org
Subject: Re: [CMake] Problem with creating shared library


I've fixed both issues, the second by implementing the pimpl idiom but still I can't used the library in the other project, again I get the error with undefined reference to the constructor (and a method that I've added):


[ 75%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
/home/dporobic/projects/testApp/main.cpp:9: undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
/home/dporobic/projects/testApp/main.cpp:11: undefined reference to `KImageAnnotator::testMethod()'
collect2: error: ld returned 1 exit status

But the constructor and the method don't seem to be private anymore:

dporobic at linux ~/projects/kImageAnnotator/build (sharedLibTestBranch)
$ nm -g lib/libkImageAnnotator.so | c++filt | grep "::testMethod\|::KImage"
0000000000014f08 T KImageAnnotator::testMethod()
0000000000014b18 T KImageAnnotator::KImageAnnotator(QPixmap const&)
0000000000014b18 T KImageAnnotator::KImageAnnotator(QPixmap const&)

Also, after the constructor call and before the call to the test method in my test project, there is a call to an inherited method (show()) from QWidget and the linker is not complaining about this one:

auto kImageAnnotator = new KImageAnnotator(pixmap);
kImageAnnotator->show();
kImageAnnotator->testMethod();


________________________________
From: Sebastián Mancilla <smancill at jlab.org>
Sent: Thursday, August 23, 2018 22:13
To: Damir Porobic
Cc: cmake at cmake.org
Subject: Re: [CMake] Problem with creating shared library

I forgot to update the include path in the patch I sent you.

It should be like this (note that it contains just "include", not "include/kImageAnnotator"):

target_include_directories(kImageAnnotator
        PUBLIC
        $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
        )

And kImageAnnotator.h should change the export include to:

#include "kImageAnnotatorExport.h"


You still have a bigger problem, though. kImageAnnotator.h is including private headers

#include "../../src/gui/VisibilitySwitcher.h"
#include "../../src/annotations/core/AnnotationArea.h"
#include "../../src/backend/Config.h"
#include "../../src/widgets/ToolPicker.h"
#include "../../src/widgets/ColorPicker.h"
#include "../../src/widgets/SizePicker.h"
#include "../../src/widgets/FillPicker.h"

If those are required by your clients, then they should be in the "include/kImageAnnotator" directory and installed along with "kImageAnnotator.h".
Otherwise, consider using the pimpl idiom and remove the includes from the public header.


El mié., 22 de ago. de 2018 a la(s) 17:08, Damir Porobic (damir_porobic at live.com<mailto:damir_porobic at live.com>) escribió:

Thanks you Sebastián for finding the issue and fixing the CMake, probably would have never found out that it was set to private.


The kImageAnnotator-example is working now as expected. Do I need something else, except installing the lib, in order to use it in different projects? I've created a second project which is identical to the kImageAnnotator-example but I get following when I try to build it:


[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
In file included from /home/dporobic/projects/testApp/main.cpp:2:0:
/usr/local/include/kImageAnnotator/KImageAnnotator.h:31:35: fatal error: KImageAnnotatorExport.h: No such file or directory
 #include <KImageAnnotatorExport.h>
                                   ^
compilation terminated.


The includes look like this:

#include <QApplication>
#include <kImageAnnotator/KImageAnnotator.h>


And the CMake looks like this:

find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp kImageAnnotator)


Am I missing something again?


________________________________
From: Sebastián Mancilla <smancill at jlab.org<mailto:smancill at jlab.org>>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; cmake at cmake.org<mailto:cmake at cmake.org>
Subject: Re: [CMake] Problem with creating shared library

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<https://urldefense.proofpoint.com/v2/url?u=https-3A__wiki.qt.io_How-5Fto-5Fcreate-5Fa-5Flibrary-5Fwith-5FQt-5Fand-5Fuse-5Fit-5Fin-5Fan-5Fapplication&d=DwMFAw&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=y6-B01YTd3TICi74QxO0LfDQVtz1m7fckmS3ItKkAmE&s=WmSjv8Q5QDreoWKNX4mS9hZm6lMR1pfhmDpjVug9nKU&e=>
http://doc.qt.io/qt-5/sharedlibra
<https://urldefense.proofpoint.com/v2/url?u=http-3A__doc.qt.io_qt-2D5_sharedlibrary.html&d=DwMFAw&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=y6-B01YTd3TICi74QxO0LfDQVtz1m7fckmS3ItKkAmE&s=I_GD4gkI-8u5o_gjXOD-wZKguDdfF2Gu4YG3Swst3hY&e=>
How to create a library with Qt and use it in an application<https://urldefense.proofpoint.com/v2/url?u=https-3A__wiki.qt.io_How-5Fto-5Fcreate-5Fa-5Flibrary-5Fwith-5FQt-5Fand-5Fuse-5Fit-5Fin-5Fan-5Fapplication&d=DwMFAw&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=y6-B01YTd3TICi74QxO0LfDQVtz1m7fckmS3ItKkAmE&s=WmSjv8Q5QDreoWKNX4mS9hZm6lMR1pfhmDpjVug9nKU&e=>
wiki.qt.io
This tutorial illustrates different approaches for using a custom library in your application on Windows. The first part explains how to create a shared library and how to link against it in your application. The second part is about creating and using a static library. Creating a shared library ...

ry.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<mailto:eric.noulard at gmail.com>) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <damir_porobic at live.com<mailto: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


--
Sebastian Mancilla
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180826/a71048e2/attachment-0001.html>


More information about the CMake mailing list