0

有我的 CMakeList.txt :

cmake_minimum_required( VERSION 2.6 )
project( FlamingSkull )
find_package(OpenThreads)
find_package(osg)
find_package(osgDB)
find_package(osgUtil)
find_package(osgViewer)
find_package(osgText)
find_package(X11)



macro( config_project PROJNAME LIBNAME )
include_directories( ${${LIBNAME}_INCLUDE_DIR} )
target_link_libraries( ${PROJNAME} ${${LIBNAME}_LIBRARY} )
endmacro()
add_executable(FlamingSkull main.cc)
config_project(FlamingSkull OPENTHREADS)
config_project(FlamingSkull OSG)
config_project(FlamingSkull OSGTEXT)
config_project(FlamingSkull OSGDB)
config_project(FlamingSkull OSGUTIL)
config_project(FlamingSkull OSGVIEWER)
config_project(FlamingSkull X11)

(这是我从头开始编写的第一个 CMakeList.txt ......)

然后在控制台中我输入cmake .并得到:

-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found OpenThreads: /usr/lib/libOpenThreads.so  
-- Found osg: /usr/lib/libosg.so  
-- Found osgDB: /usr/lib/libosgDB.so  
-- Found osgUtil: /usr/lib/libosgUtil.so  
-- Found osgViewer: /usr/lib/libosgViewer.so  
-- Found osgText: /usr/lib/libosgText.so  
-- Looking for XOpenDisplay in /usr/lib/libX11.so;/usr/lib/libXext.so
-- Looking for XOpenDisplay in /usr/lib/libX11.so;/usr/lib/libXext.so - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found X11: /usr/lib/libX11.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/theGraveyard

对我来说一切看起来都很好,但是当我输入时make,我得到:

Linking CXX executable FlamingSkull
/usr/bin/ld: CMakeFiles/FlamingSkull.dir/main.cc.o: undefined reference to symbol 'XCreateColormap'
/usr/bin/ld: note: 'XCreateColormap' is defined in DSO /usr/lib/libX11.so.6 so try adding it to the linker command line
/usr/lib/libX11.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [FlamingSkull] Error 1
make[1]: *** [CMakeFiles/FlamingSkull.dir/all] Error 2
make: *** [all] Error 2

如果我使用这样的命令,g++ ... -lX11我不会遇到麻烦。我想知道人们如何找到要传递给find_package()“包”的变量的名称是什么,我想它区分大小写......

4

2 回答 2

3

config_project 宏使用了错误的变量。您不应链接到 *_LIBRARY,它是 find 模块的内部值,而应使用 *_LIBRARIES。所以这里是固定的代码:

macro( config_project PROJNAME LIBNAME )
include_directories( ${${LIBNAME}_INCLUDE_DIR} )
target_link_libraries( ${PROJNAME} ${${LIBNAME}_LIBRARIES} )
endmacro()
于 2013-11-05T18:44:47.207 回答
1

好吧,首先你可以添加

set (CMAKE_VERBOSE_MAKEFILE ON)

到你的 CMakeLists.txt ,你会看到哪个命令失败了。

现在,从对错误消息的简要查看看来,它似乎与传递给链接器的库的顺序有关。更多细节在这里:

问题描述

于 2013-11-05T18:32:04.907 回答