CMake-未定义参考 [英] CMake - Undefined Reference

查看:111
本文介绍了CMake-未定义参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将gtest包含到我的项目中.

I am trying to include gtest to my project.

问题是我在GTest中收到未定义的引用错误.

The problem is that I get a undefined reference error in the GTest.

我正在尝试在Gtest中测试Node类.在节点的构造函数中,我正在使用类记录器.尽管我已将库记录器添加到gtest-target,但仍与记录器有关的未定义参考错误....

I am trying to test the Node class in Gtest. Inside the constructor of the Node I am using the Class Logger. Although I have added the library logger to the gtest-target I still the undefined reference error regarding to the Logger....

我的猜测是CMake不会寻找Node内使用的嵌套类.仅Node本身.

My guess CMake does no look for nested classes that are used inside Node. only Node itself.

临时修正如果我在gtest-node.cpp中使用Logger,则可以正常工作gtest.cpp

Temperoy fix If I use the Logger in the gtest-node.cpp it works gtest.cpp

/* Pseudo Code */
TEST Node
{ 
     Logger::log("Temp Fix")
     Node * n = Node(0,0,0)
}

通过这种方式,可以在gtest中直接使用Logger,从而通过cmake将logger库添加到目标中.

This way the the Logger is directly used in the gtest this way the logger-library will be add to the target by cmake.

我的设置(伪代码,因为我的项目比这个大得多)( https://github.com/ronsalm/LearningLunch )

My Setup (pseudo-code because my project is way bigger than this) (https://github.com/ronsalm/LearningLunch)

├── CMakeLists.txt
├── main.cpp
├── logger
│   ├── CMakeLists.txt
│   ├── logger.cpp
│   └── logger.h
├── Node
│   ├── CMakeLists.txt
│   ├── node.cpp
│   └── node.h
└── Gtest
    ├── CMakeLists.txt
    ├── gtest-node.cpp
    └── node.h

main.cpp

/* Pseudo Code */
int main()
{ 
     Node * n = Node(0,0,0)
}

logger.h

/* Pseudo Code */
class Logger
{ 
     log(string)
}

logger.cpp

logger.cpp

/* Pseudo Code */
Logger::log(string s)
{
//write to string to file
}

node.h

/* Pseudo Code */
class Node
{ 
     Node(int,int,int)
}

node.cpp

/* Pseudo Code */
Node::node(int x, int y , int z)
{
     Logger::log("Create Node")
}

gtest.cpp

gtest.cpp

/* Pseudo Code */
TEST Node
{ 
     Node * n = Node(0,0,0)
}

CMakeLists.txt(根)

CMakeLists.txt (Root)

project(applic)
include_directories(
      "${CMAKE_SOURE_DIR/node"
      "${CMAKE_SOURE_DIR/logger")

add_library(node node.cpp)
add_executable(applic main.cpp)
target_link_libraries(applic logger node)

CMakeLists.txt(记录器)

CMakeLists.txt (Logger)

add_library(logger logger.cpp)

CMakeLists.txt(节点)

CMakeLists.txt (Node)

add_library(node node.cpp)

CMakeLists.txt(Gtest)

CMakeLists.txt (Gtest)

add_executable(gtest-node gtest-node.cpp)
set_target_properties(gtest-node PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
target_link_libraries(gtest-logger gtest phtread logger node)
add_test(NAME node COMMAND $<TARGET_FILE:gtest-node>

enable_testing()

原始错误:

../../../../lib/libdatabase.a(sql.cpp.o): In function `SQL::Open()':
/home/rsalm/test/src/database/sql/sql.cpp:19: undefined reference to `Logger::TagDebug'
/home/rsalm/test/src/database/sql/sql.cpp:19: undefined reference to `Logger::instance(std::string const&)                                               '
../../../../lib/libdatabase.a(sql.cpp.o): In function `SQL::Close()':
/home/rsalm/test/src/database/sql/sql.cpp:27: undefined reference to `Logger::TagDebug'
/home/rsalm/test/src/database/sql/sql.cpp:27: undefined reference to `Logger::instance(std::string const&)                                               '
../../../../lib/libdatabase.a(sql.cpp.o): In function `Logger& operator<< <char [25]>(Logger&, char const (&) [25])                                               ':
/home/rsalm/test/inc/logger.h:33: undefined reference to `Logger::pInstance'
../../../../lib/libdatabase.a(sql.cpp.o): In function `Logger& operator<< <char [21]>(Logger&, char const (&) [21])                                               ':
/home/rsalm/test/inc/logger.h:33: undefined reference to `Logger::pInstance'
../../../../lib/libdatabase.a(sql.cpp.o): In function `Logger& operator<< <char [26]>(Logger&, char const (&) [26])                                               ':
/home/rsalm/test/inc/logger.h:33: undefined reference to `Logger::pInstance'
../../../../lib/libdatabase.a(sql.cpp.o): In function `Logger& operator<< <char [24]>(Logger&, char const (&) [24])                                               ':
/home/rsalm/test/inc/logger.h:33: undefined reference to `Logger::pInstance'
collect2: error: ld returned 1 exit status
src/layout/gtest/CMakeFiles/gtest-layout-factory.dir/build.make:98: recipe for target '../bin/gtest-layout-factory'                                                failed
make[2]: *** [../bin/gtest-layout-factory] Error 1
CMakeFiles/Makefile2:1824: recipe for target 'src/layout/gtest/CMakeFiles/gtest-layout-factory.dir/all' failed

推荐答案

您的 node 库使用 logger 库,但是您尚未指定该链接关系.您需要添加以下内容:

Your node library uses the logger library, but you haven't specified that link relationship. You need to add the following:

target_link_library(node PRIVATE logger)

然后,只要将 node 构建为静态库,则每次链接到 node 时,CMake都会将 logger 附加到链接库.

Then, any time you link to node, CMake will append logger to the link libraries if node is built as a static library.

几乎摆脱了链接 gtest-logger 目标的操作,在该目标中您明确地在其中添加了 logger 库,假设您实际上是在对 target_link_library()的调用中使用了 gtest-node 而不是 gtest-logger .如果您在 node 之后列出了 logger ,我希望该版本可以正常工作(假设目标名称有错字),但这并不是正确的方法.如果 gtest.cpp 中的某些内容直接引用了中的内容,则只需要显式列出 logger 作为 gtest-logger 的链接依赖项>记录器类.

You almost get away with what you did with the linking of the gtest-logger target where you explicitly added the logger library there, assuming you actually meant gtest-node rather than gtest-logger in that call to target_link_library(). If you had listed logger after node, I would have expected the build to work (assuming the target name typo), but that would not be the correct way to do it. You should only need to explicitly list logger as a link dependency of gtest-logger if something in gtest.cpp directly referenced something from the Logger class.

这篇关于CMake-未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆