qmake to Cmake transition: syntax for external librairies

亡梦爱人 提交于 2021-01-28 06:08:21

问题


For a specific project I am moving out of qmake and now have to use cmake.

My path are the following: Source : ~/Projects/Project

External static library (OSVR in this instance) paths : ~/osvr/lib/ , ~/osvr/include/osvr /osvr/include/jsoncpp

Using qmake, the linking part to that library used to be:

INCLUDEPATH += /usr/include

LIBS += -L$$PWD/../../osvr/lib/ -losvrClientKit -losvrClient -losvrCommon    -losvrUtil -ljsoncpp

INCLUDEPATH += $$PWD/../../osvr/include/
INCLUDEPATH += $$PWD/../../jsoncpp/include/
DEPENDPATH += $$PWD/../../osvr/lib/

Now I need to use cmake, but the library is not linked to:

The relevant part of my cmake.txt:

set(OSVR_DIR /home/pilou/osvr)
set(OSVR_INCLUDE_DIR /home/pilou/osvr/include/osvr/ClientKit)
find_library(OSVR_LIBRARIES ${OSVR_DIR}/lib)

[...]

target_link_libraries(myexec ${QT_LIBRARIES} ${OSVR_LIBRARIES} )
target_include_directories(myexec PUBLIC include ${OSVR_DIR}/include )

Which doesn't work...

A little help would be lovely as I am not too sure about how:

  • to ensure the external include folder is scanned

  • to link to my 3 libraries osvrClientKit osvrClient osvrCommon.

As a matter of fact I am also interested in a good explanation. Thanks in advance.

EDIT : Thanks to the reply from ComicSansMs and for the posterity, the working Cmake syntax :

set(OSVR_DIR /home/pilou/osvr)
set(OSVR_INCLUDE_DIR /home/pilou/osvr/include)
find_library(OSVR_CLIENT_KIT_LIBRARY osvrClientKit HINTS ${OSVR_DIR}/lib)
find_library(OSVR_CLIENT_LIBRARY osvrClient HINTS ${OSVR_DIR}/lib)
find_library(OSVR_COMMON_LIBRARY osvrCommon HINTS ${OSVR_DIR}/lib)
find_library(OSVR_UTIL_LIBRARY osvrUtil HINTS ${OSVR_DIR}/lib)
find_library(JSONCPP_LIBRARY jsoncpp HINTS ${OSVR_DIR}/lib/x86_64-linux-gnu)
set(OSVR_LIBRARIES ${OSVR_CLIENT_KIT_LIBRARY} ${OSVR_CLIENT_LIBRARY} ${OSVR_COMMON_LIBRARY} ${OSVR_UTIL_LIBRARY} ${JSONCPP_LIBRARY})

and down the track:

target_link_libraries(myExec ${QT_LIBRARIES} ${OSVR_LIBRARIES} )
target_include_directories(myExec PUBLIC include ${OSVR_INCLUDE_DIR} )

回答1:


Your use of find_library looks wrong.

Check out the manpage for find_library. You have to give the name of the library you want to find as an argument. You can optionally provide additional hints where to find that library:

find_library(OSVR_COMMON_LIBRARY osvrCommon
             HINTS ${OSVR_DIR}/lib)

Note that you will need one separate find_library call for each library! Since your libraries seem to have interdependencies, the correct way to model them in CMake is to also add an imported target per library and then model the interdependencies on those targets correctly.

If you don't feel comfortable doing that yet, you can also add all the find libraries to a single OSVR_LIBRARIES variable in the correct order and then depend on that:

find_package(OSVR_COMMON_LIBRARY ...)
find_package(OSVR_CLIENT_LIBRARY ...)
find_package(OSVR_CLIENTKIT_LIBRARY ...)
 ...

set(OSVR_LIBRARIES ${OSVR_CLIENTKIT_LIBRARY} ${OSVR_CLIENT_LIBRARY} ${OSVR_COMMON_LIBRARY} ...)
target_link_libraries(myexec ${QT_LIBRARIES} ${OSVR_LIBRARIES})

Note though that this approach is quite fragile with regards to future changes and should in general be avoided in favor of the imported targets.

Also, be sure that you actually have proper error handling mechanisms in place for the case that your find calls do not actually find anything.



来源:https://stackoverflow.com/questions/48273378/qmake-to-cmake-transition-syntax-for-external-librairies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!