您的位置:首页 > 其它

cmake 解决错误:Cannot specify link libraries for target

2017-04-21 03:39 671 查看
最近研究cmake来配置Qt的编译方法,写好了CMakeLists.txt通过编译后却无法链接成功;由于用的是mac osx,还以为是不同系统链接库出了问题;检查他给出的路径(变量${QT_LIBRARIES}的内容)为:

/Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtGui.framework/Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtCore.framework

手动设置为静态库的路径:

TARGET_LINK_LIBRARIES(FilterSamesetBasic
/Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtGui.la
/Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtCore.la )

还是链接失败,错误如下:

Linking CXX executable FilterSamesetBasic
ld: warning: ld: warning: ignoring file /Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtGui.la, file was built for unsupported file format ( 0x23 0x20 0x51 0x74 0x47 0x75 0x69 0x2e 0x6c 0x61 0x20 0x2d 0x20 0x61 0x20 0x6c ) which is not the architecture being linked (x86_64): /Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtGui.laignoring file /Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtCore.la, file was built for unsupported file format ( 0x23 0x20 0x51 0x74 0x43 0x6f 0x72 0x65 0x2e 0x6c 0x61 0x20 0x2d 0x20 0x61 0x20 ) which is not the architecture being linked (x86_64): /Volumes/Design/LocalSoft/Qt/Desktop/Qt/4.8.1/gcc/lib/QtCore.la

看来这个静态库是不合适的,还是仔细琢磨出错的那句话:

CMake Error at src/CMakeLists.txt:19 (TARGET_LINK_LIBRARIES):
  Cannot specify link libraries for target "FilterSamesetBasic" which is not
  built by this project.

还是疯狂查资料,最后在http://answers.ros.org/question/10890/cannot-specify-link-libraries-for-target/ 上,看到高手的回复,弄懂了问题所在:

In principle your file should work. The only thing is that you need the target (rosbuild_add_executable) before the target_link_libraries.

也就是说 要将 ADD_EXECUTABLE的声明放到设置库文件的TARGET_LINK_LIBRARIES之前,修改后终于链接成功了,可以使用啦~~,(新手不懂得很多啊!!!)附上核心内容和注视,以作纪念:

FIND_PACKAGE(Qt4 REQUIRED)
ADD_DEFINITIONS(${QT_DEFINITIONS})
INCLUDE(${QT_USE_FILE})

# 下面的是路径测试
#MESSAGE(STATUS "The BINARY dir is: " ${PROJECT_BINARY_DIR})
#MESSAGE(STATUS "The SOURCE dir is: " ${PROJECT_SOURCE_DIR})

# 设置输出程序的路径,为编译目录下的bin目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

SET(FilterSamesetBasic_SOURCES main.cpp mainwidget.cpp)
SET(FilterSamesetBasic_HEADERS mainwidget.h)
SET(FilterSamesetBasic_FORMS ../ui/mainwidget.ui)

QT4_WRAP_CPP(FilterSamesetBasic_HEADERS_MOC ${FilterSamesetBasic_HEADERS})
QT4_WRAP_UI(FilterSamesetBasic_FORMS_HEADERS ${FilterSamesetBasic_FORMS})

# 添加编译路径到包含的路径中,否则部分编译出的头文件找不到
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

ADD_EXECUTABLE(FilterSamesetBasic ${FilterSamesetBasic_SOURCES}
${FilterSamesetBasic_FORMS_HEADERS}
${FilterSamesetBasic_HEADERS_MOC}
)

# 添加共享库路,注意该配置必须添加在设置应用程序后,即ADD_EXECUTABLE后面
IF(LINUX)
MESSAGE(STATUS "Found LINUX OS")
TARGET_LINK_LIBRARIES(FilterSamesetBasic ${QT_LIBRARIES})
ELSEIF(APPLE)
MESSAGE(STATUS "Found APPLE OS")
TARGET_LINK_LIBRARIES(FilterSamesetBasic ${QT_LIBRARIES})
ENDIF()
#LINK_DIRECTORIES(${QT_LIBRARIES})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐