首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMake链接错误(未定义的引用)

CMake链接错误(未定义的引用)
EN

Stack Overflow用户
提问于 2013-05-15 20:11:59
回答 3查看 86.1K关注 0票数 24

我正在使用SSL-Vision软件。它有一个我一直试图从整个项目中分离出来的示例客户端。我找到了自己编辑客户端所需的源代码,所以我只是从软件中复制了它们,并使用CMake构建了我的客户机。

下面的项目结构简化了,缩小了范围(我相信!)

代码语言:javascript
复制
.
├── CMakeLists.txt 
├── main.cc
├── build
│   ├── CMakeLists.txt
│   └── messages_ssl_... (.cc/.h, 4 each)
└── src
    ├── CMakeLists.txt
    └── (Other subdirs and sources/headers) 

./CMakeLists.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 2.6)
project( TestClient )

find_package( PkgConfig REQUIRED )
pkg_check_modules( QTCORE_PKG QtCore )
include_directories( ${QTCORE_PKG_INCLUDE_DIRS} )

include(FindProtobuf)
find_package( Protobuf REQUIRED )
include_directories( ${PROTOBUF_INCLUDE_DIRS} )

find_package( PkgConfig REQUIRED )
pkg_check_modules( GLIB_PKG glib-2.0 ) 
include_directories( ${GLIB_PKG_INCLUDE_DIRS} )

include_directories( "src" ) 
add_subdirectory( src )

include_directories( "build" ) 
add_subdirectory( build )


add_executable( clientTest clientTest.cc )

target_link_libraries( clientTest robocup_ssl_client messages_robocup_ssl_detection.pb messages_robocup_ssl_geometry.pb messages_robocup_ssl_wrapper.pb messages_robocup_ssl_refbox_log.pb netraw robocup_ssl_client protobuf QtCore )

./build/CMakeLists.txt

代码语言:javascript
复制
add_library( messages_robocup_ssl_detection.pb SHARED messages_robocup_ssl_detection.pb.cc )

add_library( messages_robocup_ssl_refbox_log.pb SHARED messages_robocup_ssl_refbox_log.pb.cc )

add_library( messages_robocup_ssl_geometry.pb SHARED messages_robocup_ssl_geometry.pb.cc )

add_library( messages_robocup_ssl_wrapper.pb SHARED messages_robocup_ssl_wrapper.pb.cc )

这可能是messages_ssl_...文件中缺少的一个messages_ssl_...,但它们都是自动生成的,而且似乎都是正确的。

messages_robocup_ssl_detection.pb.hmessages_robocup_ssl_detection.pb.h中,只有protobuf包含。

messages_robocup_ssl_refbox_log.pb.h

代码语言:javascript
复制
#include "messages_robocup_ssl_detection.pb.h"
// Other protobuf includes

messages_robocup_ssl_wrapper.h

代码语言:javascript
复制
#include "messages_robocup_ssl_detection.pb.h"
#include "messages_robocup_ssl_geometry.pb.h"
// Other protobuf includes

在每个.cc文件中,只包含它的头和其他protobuf。

最后,当我做的时候,会产生以下错误:

代码语言:javascript
复制
Linking CXX executable clientTest
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::ByteSize() const'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::MergeFrom(SSL_GeometryData const&)'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `protobuf_AddDesc_messages_5frobocup_5fssl_5fgeometry_2eproto()'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::Clear()'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::SSL_GeometryData()'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::default_instance()'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::SerializeWithCachedSizesToArray(unsigned char*) const'
build/libmessages_robocup_ssl_wrapper.pb.so: undefined reference to `SSL_GeometryData::MergePartialFromCodedStream(google::protobuf::io::CodedInputStream*)'
collect2: ld returned 1 exit status
make[2]: ** [clientTest] Erro 1
make[1]: ** [CMakeFiles/clientTest.dir/all] Erro 2
make: ** [all] Erro 2

我已经试着解决这个问题有一段时间了。如果libmessages_robocup_ssl_wrapper.pb.so已经在链接之前构建,为什么它会显示错误呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-15 20:26:12

这很可能是连接顺序。

看起来messages_robocup_ssl_wrapper.pb依赖于messages_robocup_ssl_geometry.pb。如果是这样的话,包装器应该在链接线的几何学之前。

代码语言:javascript
复制
target_link_libraries( clientTest robocup_ssl_client
                       messages_robocup_ssl_detection.pb
                       messages_robocup_ssl_wrapper.pb
                       messages_robocup_ssl_geometry.pb
                       messages_robocup_ssl_refbox_log.pb
                       netraw
                       robocup_ssl_client
                       protobuf
                       QtCore )

更好的是,让CMake处理像这样的依赖关系。

如果你加上..。

代码语言:javascript
复制
target_link_libraries( messages_robocup_ssl_wrapper.pb
                       messages_robocup_ssl_geometry.pb )

然后,当CMake被指定为另一个目标的依赖项时,messages_robocup_ssl_wrapper.pb将自动保留该依赖项。如果这样做,则可以选择从messages_robocup_ssl_geometry.pb调用中省略target_link_libraries( clientTest ... )

票数 33
EN

Stack Overflow用户

发布于 2019-03-08 17:17:03

然而,观察这个错误的另一个原因,也许是一个不太常见的错误,是因为ABI不兼容。

例如,一个库文件可以使用标志-D_GLIBCXX_USE_CXX11_ABI=0构建,而项目不是。这可能会很难调试,特别是当不知道这可能是问题所在的时候。

我意识到,这不是您的具体场景中的问题,但这个答案可能会帮助其他人绊倒它。

票数 18
EN

Stack Overflow用户

发布于 2016-03-09 15:33:04

undefined reference to ...的另一个原因可能是在不是头的源文件中标记为inline的函数。当启用优化时,编译器实际上可以内联该函数并跳过生成导致错误的符号。

对于这种情况,解决方案是要么将内联函数移动到标头,要么删除内联标记。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16574113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档