Problem with creating shared library

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with creating shared library

Damir Porobic

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Thanks in advance!

Regards,
Damir



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Eric Noulard


Le mer. 15 août 2018 à 10:32, Damir Porobic <[hidden email]> a écrit :

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Try to compile in verbose mode 

make VERBOSE=1

and have a look at the culprit link line.
You should see the reference to your previously built shared lib.

Check whether this lib is where it should be (may be you didn't install it?)

then check which kImageAnnotatorConfig.cmake file is used by the testApp project in order to see if any
[wrong and not up to date] test & trial version of this file is lying around.
 

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Fwd: Problem with creating shared library

Eric Noulard


---------- Forwarded message ---------
From: Eric Noulard <[hidden email]>
Date: mer. 15 août 2018 à 17:13
Subject: Re: [CMake] Problem with creating shared library
To: <[hidden email]>


May be check the list of symbol in the lib.
KImageAnnotator::KImageAnnotator(QPixmap const&)
may really not be there (only declared in some header and not defined anywhere).

Does the KImageAnnotator library have unit tests in its project?
If no executable linked to the ilb you won't discover the link error until you try to link something to the lib.

This is a kind wild guess, don't take it personally :-)

Le mer. 15 août 2018 à 16:21, Damir Porobic <[hidden email]> a écrit :

Thanks for the feedback.


It looks like this line is failing:

/usr/bin/c++    -std=c++0x -fno-operator-names -fno-exceptions -Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef -Wnon-virtual-dtor -Woverloaded-virtual -Werror=return-type  -Wl,--enable-new-dtags  CMakeFiles/testApp.dir/main.cpp.o CMakeFiles/testApp.dir/testApp_automoc.cpp.o  -o testApp /usr/local/lib64/libkImageAnnotator.so.0.0.1 /usr/lib64/libKF5CoreAddons.so.5.32.0 /usr/lib64/libKF5I18n.so.5.32.0 /usr/lib64/libKF5WidgetsAddons.so.5.32.0 /usr/lib64/libQt5Widgets.so.5.6.2 /usr/lib64/libQt5Gui.so.5.6.2 /usr/lib64/libQt5Core.so.5.6.2 -Wl,-rpath,/usr/local/lib64


The lib at /usr/local/lib64/libkImageAnnotator.so.0.0.1 seems to be correct, according to the sudo make install output from the lib


Install the project...
-- Install configuration: "Debug"
-- Installing: /usr/local/lib64/libkImageAnnotator.so.0.0.1
-- Installing: /usr/local/lib64/libkImageAnnotator.so.1
-- Set runtime path of "/usr/local/lib64/libkImageAnnotator.so.0.0.1" to "/usr/local/lib64"
-- Installing: /usr/local/lib64/libkImageAnnotator.so
...
-- Installing: /usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake
-- Installing: /usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake



Don't know if this is OK, but it looks like the debug and the non-debug cmake versions are used:

/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(3):  if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} LESS 2.5 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(6):  cmake_policy(PUSH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(7):  cmake_policy(VERSION 2.6 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(13):  set(CMAKE_IMPORT_FILE_VERSION 1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(16):  set(_targetsDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(17):  set(_targetsNotDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(18):  set(_expectedTargets )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(19):  foreach(_expectedTarget kImageAnnotator )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(20):  list(APPEND _expectedTargets ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(21):  if(NOT TARGET ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(22):  list(APPEND _targetsNotDefined ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(24):  if(TARGET ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(28):  if(${_targetsDefined} STREQUAL ${_expectedTargets} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(36):  if(NOT ${_targetsDefined} STREQUAL  )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(39):  unset(_targetsDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(40):  unset(_targetsNotDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(41):  unset(_expectedTargets )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(45):  get_filename_component(_IMPORT_PREFIX ${CMAKE_CURRENT_LIST_FILE} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(46):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(47):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(48):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(49):  if(_IMPORT_PREFIX STREQUAL / )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(54):  add_library(kImageAnnotator SHARED IMPORTED )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(56):  set_target_properties(kImageAnnotator PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${_IMPORT_PREFIX}/include INTERFACE_LINK_LIBRARIES Qt5::Widgets;KF5::CoreAddons;KF5::I18n;KF5::WidgetsAddons )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(61):  if(CMAKE_VERSION VERSION_LESS 2.8.12 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(66):  get_filename_component(_DIR ${CMAKE_CURRENT_LIST_FILE} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(67):  file(GLOB CONFIG_FILES ${_DIR}/kImageAnnotatorConfig-*.cmake )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(68):  foreach(f ${CONFIG_FILES} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(69):  include(${f} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(6):  set(CMAKE_IMPORT_FILE_VERSION 1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(9):  set_property(TARGET kImageAnnotator APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(10):  set_target_properties(kImageAnnotator PROPERTIES IMPORTED_LOCATION_DEBUG ${_IMPORT_PREFIX}/lib64/libkImageAnnotator.so.0.0.1 IMPORTED_SONAME_DEBUG libkImageAnnotator.so.1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(15):  list(APPEND _IMPORT_CHECK_TARGETS kImageAnnotator )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(16):  list(APPEND _IMPORT_CHECK_FILES_FOR_kImageAnnotator ${_IMPORT_PREFIX}/lib64/libkImageAnnotator.so.0.0.1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(19):  set(CMAKE_IMPORT_FILE_VERSION )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(73):  set(_IMPORT_PREFIX )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(76):  foreach(target ${_IMPORT_CHECK_TARGETS} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(77):  foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(78):  if(NOT EXISTS ${file} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(90):  unset(_IMPORT_CHECK_FILES_FOR_${target} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(92):  unset(_IMPORT_CHECK_TARGETS )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(98):  set(CMAKE_IMPORT_FILE_VERSION )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(99):  cmake_policy(POP )

It looks like the correct cmake files are used.

Any more ideas?

Regards,
Damir


From: Eric Noulard <[hidden email]>
Sent: Wednesday, August 15, 2018 12:18
To: [hidden email]
Cc: CMake Mailinglist
Subject: Re: [CMake] Problem with creating shared library
 


Le mer. 15 août 2018 à 10:32, Damir Porobic <[hidden email]> a écrit :

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Try to compile in verbose mode 

make VERBOSE=1

and have a look at the culprit link line.
You should see the reference to your previously built shared lib.

Check whether this lib is where it should be (may be you didn't install it?)

then check which kImageAnnotatorConfig.cmake file is used by the testApp project in order to see if any
[wrong and not up to date] test & trial version of this file is lying around.
 

--
Eric


--
Eric


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Problem with creating shared library

Damir Porobic

It does have unit tests and an executable which I use for testing, all that works fine.


I have the feeling that something with the CMakeList config is not alright. Is there any tutorial that explains how to correctly create a shared library?



From: CMake <[hidden email]> on behalf of Eric Noulard <[hidden email]>
Sent: Wednesday, August 15, 2018 17:13
To: CMake Mailinglist
Subject: [CMake] Fwd: Problem with creating shared library
 


---------- Forwarded message ---------
From: Eric Noulard <[hidden email]>
Date: mer. 15 août 2018 à 17:13
Subject: Re: [CMake] Problem with creating shared library
To: <[hidden email]>


May be check the list of symbol in the lib.
KImageAnnotator::KImageAnnotator(QPixmap const&)
may really not be there (only declared in some header and not defined anywhere).

Does the KImageAnnotator library have unit tests in its project?
If no executable linked to the ilb you won't discover the link error until you try to link something to the lib.

This is a kind wild guess, don't take it personally :-)

Le mer. 15 août 2018 à 16:21, Damir Porobic <[hidden email]> a écrit :

Thanks for the feedback.


It looks like this line is failing:

/usr/bin/c++    -std=c++0x -fno-operator-names -fno-exceptions -Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef -Wnon-virtual-dtor -Woverloaded-virtual -Werror=return-type  -Wl,--enable-new-dtags  CMakeFiles/testApp.dir/main.cpp.o CMakeFiles/testApp.dir/testApp_automoc.cpp.o  -o testApp /usr/local/lib64/libkImageAnnotator.so.0.0.1 /usr/lib64/libKF5CoreAddons.so.5.32.0 /usr/lib64/libKF5I18n.so.5.32.0 /usr/lib64/libKF5WidgetsAddons.so.5.32.0 /usr/lib64/libQt5Widgets.so.5.6.2 /usr/lib64/libQt5Gui.so.5.6.2 /usr/lib64/libQt5Core.so.5.6.2 -Wl,-rpath,/usr/local/lib64


The lib at /usr/local/lib64/libkImageAnnotator.so.0.0.1 seems to be correct, according to the sudo make install output from the lib


Install the project...
-- Install configuration: "Debug"
-- Installing: /usr/local/lib64/libkImageAnnotator.so.0.0.1
-- Installing: /usr/local/lib64/libkImageAnnotator.so.1
-- Set runtime path of "/usr/local/lib64/libkImageAnnotator.so.0.0.1" to "/usr/local/lib64"
-- Installing: /usr/local/lib64/libkImageAnnotator.so
...
-- Installing: /usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake
-- Installing: /usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake



Don't know if this is OK, but it looks like the debug and the non-debug cmake versions are used:

/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(3):  if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} LESS 2.5 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(6):  cmake_policy(PUSH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(7):  cmake_policy(VERSION 2.6 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(13):  set(CMAKE_IMPORT_FILE_VERSION 1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(16):  set(_targetsDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(17):  set(_targetsNotDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(18):  set(_expectedTargets )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(19):  foreach(_expectedTarget kImageAnnotator )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(20):  list(APPEND _expectedTargets ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(21):  if(NOT TARGET ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(22):  list(APPEND _targetsNotDefined ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(24):  if(TARGET ${_expectedTarget} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(28):  if(${_targetsDefined} STREQUAL ${_expectedTargets} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(36):  if(NOT ${_targetsDefined} STREQUAL  )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(39):  unset(_targetsDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(40):  unset(_targetsNotDefined )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(41):  unset(_expectedTargets )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(45):  get_filename_component(_IMPORT_PREFIX ${CMAKE_CURRENT_LIST_FILE} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(46):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(47):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(48):  get_filename_component(_IMPORT_PREFIX ${_IMPORT_PREFIX} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(49):  if(_IMPORT_PREFIX STREQUAL / )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(54):  add_library(kImageAnnotator SHARED IMPORTED )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(56):  set_target_properties(kImageAnnotator PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${_IMPORT_PREFIX}/include INTERFACE_LINK_LIBRARIES Qt5::Widgets;KF5::CoreAddons;KF5::I18n;KF5::WidgetsAddons )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(61):  if(CMAKE_VERSION VERSION_LESS 2.8.12 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(66):  get_filename_component(_DIR ${CMAKE_CURRENT_LIST_FILE} PATH )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(67):  file(GLOB CONFIG_FILES ${_DIR}/kImageAnnotatorConfig-*.cmake )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(68):  foreach(f ${CONFIG_FILES} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(69):  include(${f} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(6):  set(CMAKE_IMPORT_FILE_VERSION 1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(9):  set_property(TARGET kImageAnnotator APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(10):  set_target_properties(kImageAnnotator PROPERTIES IMPORTED_LOCATION_DEBUG ${_IMPORT_PREFIX}/lib64/libkImageAnnotator.so.0.0.1 IMPORTED_SONAME_DEBUG libkImageAnnotator.so.1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(15):  list(APPEND _IMPORT_CHECK_TARGETS kImageAnnotator )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(16):  list(APPEND _IMPORT_CHECK_FILES_FOR_kImageAnnotator ${_IMPORT_PREFIX}/lib64/libkImageAnnotator.so.0.0.1 )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig-debug.cmake(19):  set(CMAKE_IMPORT_FILE_VERSION )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(73):  set(_IMPORT_PREFIX )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(76):  foreach(target ${_IMPORT_CHECK_TARGETS} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(77):  foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(78):  if(NOT EXISTS ${file} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(90):  unset(_IMPORT_CHECK_FILES_FOR_${target} )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(92):  unset(_IMPORT_CHECK_TARGETS )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(98):  set(CMAKE_IMPORT_FILE_VERSION )
/usr/local/share/kImageAnnotatorConfig/cmake/kImageAnnotatorConfig.cmake(99):  cmake_policy(POP )

It looks like the correct cmake files are used.

Any more ideas?

Regards,
Damir


From: Eric Noulard <[hidden email]>
Sent: Wednesday, August 15, 2018 12:18
To: [hidden email]
Cc: CMake Mailinglist
Subject: Re: [CMake] Problem with creating shared library
 


Le mer. 15 août 2018 à 10:32, Damir Porobic <[hidden email]> a écrit :

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Try to compile in verbose mode 

make VERBOSE=1

and have a look at the culprit link line.
You should see the reference to your previously built shared lib.

Check whether this lib is where it should be (may be you didn't install it?)

then check which kImageAnnotatorConfig.cmake file is used by the testApp project in order to see if any
[wrong and not up to date] test & trial version of this file is lying around.
 

--
Eric


--
Eric


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Sebastián Mancilla
In reply to this post by Damir Porobic
You are mixing the config file and the targets file.

The config file is a template that you normally put in cmake/FooConfig.cmake.in

You copy the template into the binary dir:

    include(CMakePackageConfigHelpers)

    set(INSTALL_CONFIGDIR lib/cmake/Foo)

    configure_package_config_file(
      "${CMAKE_CURRENT_LIST_DIR}/cmake/FooConfig.cmake.in"
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
      INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
    )

It is a good idea to create a version file:
   
    write_basic_package_version_file(
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      VERSION ${PROJECT_VERSION}
      COMPATIBILITY SameMajorVersion
   )

And then install both:

    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

For the targets is a different file. When you install the library you should use

    install(TARGETS Foo EXPORT FooTargets ...)

And then export and install the targets:

    # Into the build tree
    export(EXPORT FooTargets
      FILE "${CMAKE_CURRENT_BINARY_DIR}/FooTargets.cmake"
      NAMESPACE Foo::
    )
    
    # Into PREFIX
    install(EXPORT FooTargets
      FILE FooTargets.cmake
      NAMESPACE Foo::
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

Finally, your template FooConfig.cmake.in should look like this:

  include(CMakeFindDependencyMacro)

  @PACKAGE_INIT@
 
  # list your required dependencies here
  find_dependency(Threads)

  if(NOT TARGET Foo::Foo)
    include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")
  endif()

All this is pretty much the same for any project. Here are the best links explaining it:





El mié., 15 de ago. de 2018 a la(s) 05:32, Damir Porobic ([hidden email]) escribió:

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Thanks in advance!

Regards,
Damir


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cmake.org_Wiki_CMake-5FFAQ&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=JJ5RqPyjdlGGc3fT-5nQQM-JcJzwjBZU6ciOQlmEJVs&e=

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_support.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=vUHQvc_7Ovi_5BDjy3JYFOIvTmihTSFOQNndNSpMOnA&e=
CMake Consulting: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_consulting.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=91AJA3BxTfHAvsi0mAzkszAyUUmE2xfwbLgN_fYvFO4&e=
CMake Training Courses: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_training.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=isQW6paMIqhUFaejOZ4qfUiSVwNiR1yxoQn4J91yb8o&e=

Visit other Kitware open-source projects at https://urldefense.proofpoint.com/v2/url?u=http-3A__www.kitware.com_opensource_opensource.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=ose8YVW10s6tWkujUCq162vyyipCdlw-MW93qUlqdGk&e=

Follow this link to subscribe/unsubscribe:
https://urldefense.proofpoint.com/v2/url?u=https-3A__cmake.org_mailman_listinfo_cmake&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=KftR51q4EGgNERicS2QyHvzlrNaqb11IUwwbz1YTjVU&e=


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

Thanks Sebastián for providing those links!


I've followed the second link that you've provided but somehow I'm still running into issue.


I've changed the directory structure to this:

- kImageAnnotator
    - cmake
        - kImageAnnotator-config.cmake.in
    - example
        - main.cpp             
        - CMakeLists.txt
    - include
        - kImageAnnotator
            - KImageAnnotator.h     // the main api header
    - src
        - KImageAnnotator.cpp
        - "And all other .cpp and .h files"
        - CMakeLists.txt
    - CMakeLists.txt

src/CMakeList.txt looks like this:
add_library(kImageAnnotator ${kimageannotator_SRCS} ${CMAKE_SOURCE_DIR}/include/kImageAnnotator/KImageAnnotator.h)

add_library(kImageAnnotator::kImageAnnotator ALIAS kImageAnnotator)

option(BUILD_SHARED_LIBS "Build shared library" ON)
include(GenerateExportHeader)

generate_export_header(kImageAnnotator
        EXPORT_MACRO_NAME KIMAGEANNOTATOR_API
        EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include/KImageAnnotator.h
        )

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

set_target_properties(kImageAnnotator PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        )

find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

target_link_libraries(kImageAnnotator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

include(GNUInstallDirs)

install(TARGETS kImageAnnotator
        EXPORT kImageAnnotator-targets
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        INCLUDES DESTINATION ${LIBLEGACY_INCLUDE_DIRS}
)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/kImageAnnotator
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT kImageAnnotator-targets
        FILE kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)


include(CMakePackageConfigHelpers)

configure_package_config_file(
        ${CMAKE_SOURCE_DIR}/cmake/kImageAnnotator-config.cmake.in
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

write_basic_package_version_file(
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        VERSION ${PROJECT_VERSION}
        COMPATIBILITY AnyNewerVersion
)

install(FILES
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

export(EXPORT kImageAnnotator-targets
        FILE ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
)


kImageAnnotator-config.cmake.in looks like this:

include(CMakeFindDependencyMacro)

@PACKAGE_INIT@

# find_dependency(Threads)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

if(NOT TARGET kImageAnnotator::kImageAnnotator)
  include("${CMAKE_CURRENT_LIST_DIR}/kImageAnnotator-targets.cmake")
endif()



example/CMakeLists.txt looks like this:

add_executable(kImageAnnotator-example ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

target_link_libraries(kImageAnnotator-example PRIVATE kImageAnnotator::kImageAnnotator)



And example/main.cpp looks like this:

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

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QPixmap pixmap(QSize(500, 500));
    pixmap.fill(QColor(Qt::darkGreen));
    auto kImageAnnotator = new KImageAnnotator(pixmap);
    kImageAnnotator->show();

    return app.exec();
}



Still I'm getting following error when trying to build kImageAnnotator-example:

Scanning dependencies of target kImageAnnotator-example
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/main.cpp.o
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/kImageAnnotator-example_autogen/mocs_compilation.cpp.o
[100%] Linking CXX executable kImageAnnotator-example
CMakeFiles/kImageAnnotator-example.dir/main.cpp.o: In function `main':
/home/dporobic/projects/kImageAnnotator/example/main.cpp:29: undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
example/CMakeFiles/kImageAnnotator-example.dir/build.make:98: recipe for target 'example/kImageAnnotator-example' failed

Any idea what I'm doing wrong? It looks like the same issue that I was having earlier.



From: Sebastián Mancilla <[hidden email]>
Sent: Wednesday, August 15, 2018 21:48
To: [hidden email]
Cc: [hidden email]
Subject: Re: [CMake] Problem with creating shared library
 
You are mixing the config file and the targets file.

The config file is a template that you normally put in cmake/FooConfig.cmake.in

You copy the template into the binary dir:

    include(CMakePackageConfigHelpers)

    set(INSTALL_CONFIGDIR lib/cmake/Foo)

    configure_package_config_file(
      "${CMAKE_CURRENT_LIST_DIR}/cmake/FooConfig.cmake.in"
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
      INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
    )

It is a good idea to create a version file:
   
    write_basic_package_version_file(
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      VERSION ${PROJECT_VERSION}
      COMPATIBILITY SameMajorVersion
   )

And then install both:

    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

For the targets is a different file. When you install the library you should use

    install(TARGETS Foo EXPORT FooTargets ...)

And then export and install the targets:

    # Into the build tree
    export(EXPORT FooTargets
      FILE "${CMAKE_CURRENT_BINARY_DIR}/FooTargets.cmake"
      NAMESPACE Foo::
    )
    
    # Into PREFIX
    install(EXPORT FooTargets
      FILE FooTargets.cmake
      NAMESPACE Foo::
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

Finally, your template FooConfig.cmake.in should look like this:

  include(CMakeFindDependencyMacro)

  @PACKAGE_INIT@
 
  # list your required dependencies here
  find_dependency(Threads)

  if(NOT TARGET Foo::Foo)
    include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")
  endif()

All this is pretty much the same for any project. Here are the best links explaining it:





El mié., 15 de ago. de 2018 a la(s) 05:32, Damir Porobic ([hidden email]) escribió:

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Thanks in advance!

Regards,
Damir


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cmake.org_Wiki_CMake-5FFAQ&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=JJ5RqPyjdlGGc3fT-5nQQM-JcJzwjBZU6ciOQlmEJVs&e=

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_support.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=vUHQvc_7Ovi_5BDjy3JYFOIvTmihTSFOQNndNSpMOnA&e=
CMake Consulting: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_consulting.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=91AJA3BxTfHAvsi0mAzkszAyUUmE2xfwbLgN_fYvFO4&e=
CMake Training Courses: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_training.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=isQW6paMIqhUFaejOZ4qfUiSVwNiR1yxoQn4J91yb8o&e=

Visit other Kitware open-source projects at https://urldefense.proofpoint.com/v2/url?u=http-3A__www.kitware.com_opensource_opensource.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=ose8YVW10s6tWkujUCq162vyyipCdlw-MW93qUlqdGk&e=

Follow this link to subscribe/unsubscribe:
https://urldefense.proofpoint.com/v2/url?u=https-3A__cmake.org_mailman_listinfo_cmake&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=KftR51q4EGgNERicS2QyHvzlrNaqb11IUwwbz1YTjVU&e=


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Eric Noulard
Hi Damir,

May be the issue has nothing to do with the way you build the exported <>Config.cmake files.
Since may be some nasty detail slipped in, could you tell us if the project is public.
It looks like this one:

may be you can push a branch that contains the exact thing that fails on your side and it may be easier to review the detail?

on the master of this repo there does not seem to have any "example" dir.

However you already have hand written header file:

which has the very same name as the "exported header you generate":
generate_export_header(kImageAnnotator
        EXPORT_MACRO_NAME KIMAGEANNOTATOR_API
        EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include/KImageAnnotator.h
        )

so may be 
KImageAnnotator::KImageAnnotator(QPixmap const&)
is exposed by the hand-written file and not by the generated one so
that your unit test links ok but your "example" does not ?

Did you check the content of the generated header file?








Le dim. 19 août 2018 à 15:17, Damir Porobic <[hidden email]> a écrit :

Thanks Sebastián for providing those links!


I've followed the second link that you've provided but somehow I'm still running into issue.


I've changed the directory structure to this:

- kImageAnnotator
    - cmake
        - kImageAnnotator-config.cmake.in
    - example
        - main.cpp             
        - CMakeLists.txt
    - include
        - kImageAnnotator
            - KImageAnnotator.h     // the main api header
    - src
        - KImageAnnotator.cpp
        - "And all other .cpp and .h files"
        - CMakeLists.txt
    - CMakeLists.txt

src/CMakeList.txt looks like this:
add_library(kImageAnnotator ${kimageannotator_SRCS} ${CMAKE_SOURCE_DIR}/include/kImageAnnotator/KImageAnnotator.h)

add_library(kImageAnnotator::kImageAnnotator ALIAS kImageAnnotator)

option(BUILD_SHARED_LIBS "Build shared library" ON)
include(GenerateExportHeader)

generate_export_header(kImageAnnotator
        EXPORT_MACRO_NAME KIMAGEANNOTATOR_API
        EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include/KImageAnnotator.h
        )

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

set_target_properties(kImageAnnotator PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        )

find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

target_link_libraries(kImageAnnotator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

include(GNUInstallDirs)

install(TARGETS kImageAnnotator
        EXPORT kImageAnnotator-targets
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        INCLUDES DESTINATION ${LIBLEGACY_INCLUDE_DIRS}
)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/kImageAnnotator
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT kImageAnnotator-targets
        FILE kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)


include(CMakePackageConfigHelpers)

configure_package_config_file(
        ${CMAKE_SOURCE_DIR}/cmake/kImageAnnotator-config.cmake.in
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

write_basic_package_version_file(
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        VERSION ${PROJECT_VERSION}
        COMPATIBILITY AnyNewerVersion
)

install(FILES
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

export(EXPORT kImageAnnotator-targets
        FILE ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
)


kImageAnnotator-config.cmake.in looks like this:

include(CMakeFindDependencyMacro)

@PACKAGE_INIT@

# find_dependency(Threads)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

if(NOT TARGET kImageAnnotator::kImageAnnotator)
  include("${CMAKE_CURRENT_LIST_DIR}/kImageAnnotator-targets.cmake")
endif()



example/CMakeLists.txt looks like this:

add_executable(kImageAnnotator-example ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

target_link_libraries(kImageAnnotator-example PRIVATE kImageAnnotator::kImageAnnotator)



And example/main.cpp looks like this:

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

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QPixmap pixmap(QSize(500, 500));
    pixmap.fill(QColor(Qt::darkGreen));
    auto kImageAnnotator = new KImageAnnotator(pixmap);
    kImageAnnotator->show();

    return app.exec();
}



Still I'm getting following error when trying to build kImageAnnotator-example:

Scanning dependencies of target kImageAnnotator-example
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/main.cpp.o
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/kImageAnnotator-example_autogen/mocs_compilation.cpp.o
[100%] Linking CXX executable kImageAnnotator-example
CMakeFiles/kImageAnnotator-example.dir/main.cpp.o: In function `main':
/home/dporobic/projects/kImageAnnotator/example/main.cpp:29: undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
example/CMakeFiles/kImageAnnotator-example.dir/build.make:98: recipe for target 'example/kImageAnnotator-example' failed

Any idea what I'm doing wrong? It looks like the same issue that I was having earlier.



From: Sebastián Mancilla <[hidden email]>
Sent: Wednesday, August 15, 2018 21:48
To: [hidden email]
Cc: [hidden email]
Subject: Re: [CMake] Problem with creating shared library
 
You are mixing the config file and the targets file.

The config file is a template that you normally put in cmake/FooConfig.cmake.in

You copy the template into the binary dir:

    include(CMakePackageConfigHelpers)

    set(INSTALL_CONFIGDIR lib/cmake/Foo)

    configure_package_config_file(
      "${CMAKE_CURRENT_LIST_DIR}/cmake/FooConfig.cmake.in"
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
      INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
    )

It is a good idea to create a version file:
   
    write_basic_package_version_file(
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      VERSION ${PROJECT_VERSION}
      COMPATIBILITY SameMajorVersion
   )

And then install both:

    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

For the targets is a different file. When you install the library you should use

    install(TARGETS Foo EXPORT FooTargets ...)

And then export and install the targets:

    # Into the build tree
    export(EXPORT FooTargets
      FILE "${CMAKE_CURRENT_BINARY_DIR}/FooTargets.cmake"
      NAMESPACE Foo::
    )
    
    # Into PREFIX
    install(EXPORT FooTargets
      FILE FooTargets.cmake
      NAMESPACE Foo::
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

Finally, your template FooConfig.cmake.in should look like this:

  include(CMakeFindDependencyMacro)

  @PACKAGE_INIT@
 
  # list your required dependencies here
  find_dependency(Threads)

  if(NOT TARGET Foo::Foo)
    include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")
  endif()

All this is pretty much the same for any project. Here are the best links explaining it:





El mié., 15 de ago. de 2018 a la(s) 05:32, Damir Porobic ([hidden email]) escribió:

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Thanks in advance!

Regards,
Damir


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cmake.org_Wiki_CMake-5FFAQ&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=JJ5RqPyjdlGGc3fT-5nQQM-JcJzwjBZU6ciOQlmEJVs&e=

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_support.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=vUHQvc_7Ovi_5BDjy3JYFOIvTmihTSFOQNndNSpMOnA&e=
CMake Consulting: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_consulting.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=91AJA3BxTfHAvsi0mAzkszAyUUmE2xfwbLgN_fYvFO4&e=
CMake Training Courses: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_training.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=isQW6paMIqhUFaejOZ4qfUiSVwNiR1yxoQn4J91yb8o&e=

Visit other Kitware open-source projects at https://urldefense.proofpoint.com/v2/url?u=http-3A__www.kitware.com_opensource_opensource.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=ose8YVW10s6tWkujUCq162vyyipCdlw-MW93qUlqdGk&e=

Follow this link to subscribe/unsubscribe:
https://urldefense.proofpoint.com/v2/url?u=https-3A__cmake.org_mailman_listinfo_cmake&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=KftR51q4EGgNERicS2QyHvzlrNaqb11IUwwbz1YTjVU&e=


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

Hi Eric,


yes, this is the project. I have pushed my current state to this branch https://github.com/DamirPorobic/kImageAnnotator/tree/sharedLibTestBranch


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.


Cheers,

Damir


From: Eric Noulard <[hidden email]>
Sent: Sunday, August 19, 2018 22:44
To: Damir Porobic
Cc: [hidden email]; CMake Mailinglist
Subject: Re: [CMake] Problem with creating shared library
 
Hi Damir,

May be the issue has nothing to do with the way you build the exported <>Config.cmake files.
Since may be some nasty detail slipped in, could you tell us if the project is public.
It looks like this one:

may be you can push a branch that contains the exact thing that fails on your side and it may be easier to review the detail?

on the master of this repo there does not seem to have any "example" dir.

However you already have hand written header file:

which has the very same name as the "exported header you generate":
generate_export_header(kImageAnnotator
        EXPORT_MACRO_NAME KIMAGEANNOTATOR_API
        EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include/KImageAnnotator.h
        )

so may be 
KImageAnnotator::KImageAnnotator(QPixmap const&)
is exposed by the hand-written file and not by the generated one so
that your unit test links ok but your "example" does not ?

Did you check the content of the generated header file?








Le dim. 19 août 2018 à 15:17, Damir Porobic <[hidden email]> a écrit :

Thanks Sebastián for providing those links!


I've followed the second link that you've provided but somehow I'm still running into issue.


I've changed the directory structure to this:

- kImageAnnotator
    - cmake
        - kImageAnnotator-config.cmake.in
    - example
        - main.cpp             
        - CMakeLists.txt
    - include
        - kImageAnnotator
            - KImageAnnotator.h     // the main api header
    - src
        - KImageAnnotator.cpp
        - "And all other .cpp and .h files"
        - CMakeLists.txt
    - CMakeLists.txt

src/CMakeList.txt looks like this:
add_library(kImageAnnotator ${kimageannotator_SRCS} ${CMAKE_SOURCE_DIR}/include/kImageAnnotator/KImageAnnotator.h)

add_library(kImageAnnotator::kImageAnnotator ALIAS kImageAnnotator)

option(BUILD_SHARED_LIBS "Build shared library" ON)
include(GenerateExportHeader)

generate_export_header(kImageAnnotator
        EXPORT_MACRO_NAME KIMAGEANNOTATOR_API
        EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include/KImageAnnotator.h
        )

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

set_target_properties(kImageAnnotator PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        )

find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

target_link_libraries(kImageAnnotator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

include(GNUInstallDirs)

install(TARGETS kImageAnnotator
        EXPORT kImageAnnotator-targets
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        INCLUDES DESTINATION ${LIBLEGACY_INCLUDE_DIRS}
)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/kImageAnnotator
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT kImageAnnotator-targets
        FILE kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)


include(CMakePackageConfigHelpers)

configure_package_config_file(
        ${CMAKE_SOURCE_DIR}/cmake/kImageAnnotator-config.cmake.in
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

write_basic_package_version_file(
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        VERSION ${PROJECT_VERSION}
        COMPATIBILITY AnyNewerVersion
)

install(FILES
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config.cmake
        ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-config-version.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kImageAnnotator
)

export(EXPORT kImageAnnotator-targets
        FILE ${CMAKE_BINARY_DIR}/cmake/kImageAnnotator-targets.cmake
        NAMESPACE kImageAnnotator::
)


kImageAnnotator-config.cmake.in looks like this:

include(CMakeFindDependencyMacro)

@PACKAGE_INIT@

# find_dependency(Threads)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED Widgets)

find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

if(NOT TARGET kImageAnnotator::kImageAnnotator)
  include("${CMAKE_CURRENT_LIST_DIR}/kImageAnnotator-targets.cmake")
endif()



example/CMakeLists.txt looks like this:

add_executable(kImageAnnotator-example ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

target_link_libraries(kImageAnnotator-example PRIVATE kImageAnnotator::kImageAnnotator)



And example/main.cpp looks like this:

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

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QPixmap pixmap(QSize(500, 500));
    pixmap.fill(QColor(Qt::darkGreen));
    auto kImageAnnotator = new KImageAnnotator(pixmap);
    kImageAnnotator->show();

    return app.exec();
}



Still I'm getting following error when trying to build kImageAnnotator-example:

Scanning dependencies of target kImageAnnotator-example
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/main.cpp.o
[ 98%] Building CXX object example/CMakeFiles/kImageAnnotator-example.dir/kImageAnnotator-example_autogen/mocs_compilation.cpp.o
[100%] Linking CXX executable kImageAnnotator-example
CMakeFiles/kImageAnnotator-example.dir/main.cpp.o: In function `main':
/home/dporobic/projects/kImageAnnotator/example/main.cpp:29: undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
example/CMakeFiles/kImageAnnotator-example.dir/build.make:98: recipe for target 'example/kImageAnnotator-example' failed

Any idea what I'm doing wrong? It looks like the same issue that I was having earlier.



From: Sebastián Mancilla <[hidden email]>
Sent: Wednesday, August 15, 2018 21:48
To: [hidden email]
Cc: [hidden email]
Subject: Re: [CMake] Problem with creating shared library
 
You are mixing the config file and the targets file.

The config file is a template that you normally put in cmake/FooConfig.cmake.in

You copy the template into the binary dir:

    include(CMakePackageConfigHelpers)

    set(INSTALL_CONFIGDIR lib/cmake/Foo)

    configure_package_config_file(
      "${CMAKE_CURRENT_LIST_DIR}/cmake/FooConfig.cmake.in"
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
      INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
    )

It is a good idea to create a version file:
   
    write_basic_package_version_file(
      "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      VERSION ${PROJECT_VERSION}
      COMPATIBILITY SameMajorVersion
   )

And then install both:

    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake"
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

For the targets is a different file. When you install the library you should use

    install(TARGETS Foo EXPORT FooTargets ...)

And then export and install the targets:

    # Into the build tree
    export(EXPORT FooTargets
      FILE "${CMAKE_CURRENT_BINARY_DIR}/FooTargets.cmake"
      NAMESPACE Foo::
    )
    
    # Into PREFIX
    install(EXPORT FooTargets
      FILE FooTargets.cmake
      NAMESPACE Foo::
      DESTINATION ${INSTALL_CONFIGDIR}
      COMPONENT Devel
    )

Finally, your template FooConfig.cmake.in should look like this:

  include(CMakeFindDependencyMacro)

  @PACKAGE_INIT@
 
  # list your required dependencies here
  find_dependency(Threads)

  if(NOT TARGET Foo::Foo)
    include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")
  endif()

All this is pretty much the same for any project. Here are the best links explaining it:





El mié., 15 de ago. de 2018 a la(s) 05:32, Damir Porobic ([hidden email]) escribió:

Hi Folks,


I'm trying to write a shared library and run into an issue where I can't find any clues to where the problem is.

I have a project with following structure:


src/

    dir1/

        file1.h

        file1.cpp

    dir2/

        file2.h

        file2.cpp



Now I have this CMakeList:

cmake_minimum_required(VERSION 3.5)

project(kImageAnnotator VERSION 0.0.1 LANGUAGES CXX)

...


add_library(${PROJECT_NAME} SHARED ${kimageannotator_SRCS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)

set(kimageannotator_CONFIG ${PROJECT_NAME}Config)

install(TARGETS ${PROJECT_NAME} EXPORT ${kimageannotator_CONFIG}
        ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(EXPORT ${kimageannotator_CONFIG} DESTINATION share/${kimageannotator_CONFIG}/cmake)

export(TARGETS ${PROJECT_NAME} FILE ${kimageannotator_CONFIG}.cmake)


In another test project, I add the library like this:
...
find_package(kImageAnnotator REQUIRED)

add_executable(testApp main.cpp)
target_link_libraries(testApp Qt5::Widgets kImageAnnotator)


Now when I try to build my test project, I get this:

dporobic@linux ~/projects/testApp/build
$ cmake .. && make
-- Could not set up the appstream test. appstreamcli is missing.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dporobic/projects/testApp/build
[ 25%] Automatic moc for target testApp
[ 25%] Built target testApp_automoc
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/testApp.dir/testApp_automoc.cpp.o
[100%] Linking CXX executable testApp
CMakeFiles/testApp.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x8e): undefined reference to `KImageAnnotator::KImageAnnotator(QPixmap const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/testApp.dir/build.make:120: recipe for target 'testApp' failed
make[2]: *** [testApp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testApp.dir/all' failed
make[1]: *** [CMakeFiles/testApp.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

Any idea how I could/should troubleshoot such issue?

Thanks in advance!

Regards,
Damir


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cmake.org_Wiki_CMake-5FFAQ&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=JJ5RqPyjdlGGc3fT-5nQQM-JcJzwjBZU6ciOQlmEJVs&e=

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_support.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=vUHQvc_7Ovi_5BDjy3JYFOIvTmihTSFOQNndNSpMOnA&e=
CMake Consulting: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_consulting.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=91AJA3BxTfHAvsi0mAzkszAyUUmE2xfwbLgN_fYvFO4&e=
CMake Training Courses: https://urldefense.proofpoint.com/v2/url?u=http-3A__cmake.org_cmake_help_training.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=isQW6paMIqhUFaejOZ4qfUiSVwNiR1yxoQn4J91yb8o&e=

Visit other Kitware open-source projects at https://urldefense.proofpoint.com/v2/url?u=http-3A__www.kitware.com_opensource_opensource.html&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=ose8YVW10s6tWkujUCq162vyyipCdlw-MW93qUlqdGk&e=

Follow this link to subscribe/unsubscribe:
https://urldefense.proofpoint.com/v2/url?u=https-3A__cmake.org_mailman_listinfo_cmake&d=DwICAg&c=lz9TcOasaINaaC3U7FbMev2lsutwpI4--09aP8Lu18s&r=8hmSv9ww5s9qu3iT8h5WMi8-YcKXaJvelxT3fMih7S4&m=1H4kXxGuNG-DAo1qM-u8bdF6AKKHkJoiCuqwZ4QLvtY&s=KftR51q4EGgNERicS2QyHvzlrNaqb11IUwwbz1YTjVU&e=


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Eric Noulard


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Sebastián Mancilla
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

kia.patch (25K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

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 <[hidden email]>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; [hidden email]
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Sebastián Mancilla
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 ([hidden email]) 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 <[hidden email]>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; [hidden email]
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile


--
Sebastian Mancilla

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

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@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 <[hidden email]>
Sent: Thursday, August 23, 2018 22:13
To: Damir Porobic
Cc: [hidden email]
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 ([hidden email]) 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 <[hidden email]>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; [hidden email]
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile


--
Sebastian Mancilla

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

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 <[hidden email]>
Sent: Friday, August 24, 2018 9:37:30 PM
To: Sebastián Mancilla
Cc: [hidden email]
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@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 <[hidden email]>
Sent: Thursday, August 23, 2018 22:13
To: Damir Porobic
Cc: [hidden email]
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 ([hidden email]) 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 <[hidden email]>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; [hidden email]
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile


--
Sebastian Mancilla

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Problem with creating shared library

Damir Porobic

Any idea how I could fix the last issue in my shared library?

Cmake seems to expect a file with version in its name (/usr/local/lib64/libkImageAnnotator.so.0.0.1) but my cmake for the shared library only creates a file without version in it's name (/usr/local/lib64/libkImageAnnotator.so)


Manually creating a link seems to fix the issue, but I guess there is a way to let cmake generate it.


Any idea?



From: Damir Porobic <[hidden email]>
Sent: Sunday, August 26, 2018 13:50
To: Sebastián Mancilla
Cc: [hidden email]
Subject: Re: [CMake] Problem with creating shared library
 

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 <[hidden email]>
Sent: Friday, August 24, 2018 9:37:30 PM
To: Sebastián Mancilla
Cc: [hidden email]
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@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 <[hidden email]>
Sent: Thursday, August 23, 2018 22:13
To: Damir Porobic
Cc: [hidden email]
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 ([hidden email]) 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 <[hidden email]>
Sent: Wednesday, August 22, 2018 00:51
To: Eric Noulard
Cc: Damir Porobic; [hidden email]
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:


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 ([hidden email]) escribió:


Le lun. 20 août 2018 à 19:05, Damir Porobic <[hidden email]> 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


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) you seems to be using.

--
Eric


--
Sebastian Mancilla Matta
CCTVal, UTFSM
Valparaíso, Chile


--
Sebastian Mancilla

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake