【问题标题】:CMake `add_executable` and `target_link_libraries` throwing linking errorCMake `add_executable` 和 `target_link_libraries` 抛出链接错误
【发布时间】:2022-12-04 01:15:00
【问题描述】:

我通过 javidx9 关注 Asio tutorial 并使用 CMake 链接我的可执行文件和库。 this repository 中提供了完整的源代码。

我面临与文件夹中的可执行文件 Server.cppClient.cpp 的链接错误

- Source
---- Main
-------- Server.cpp
-------- Client.cpp

main 函数中,如果我创建继承自 ServerInterface 的类对象 CustomServer

int main ()
{
    CustomServer server(60000);
    return 0;
}

我收到以下链接错误:

Undefined symbols for architecture x86_64:
  "Tachys::Networking::ServerInterface<CustomMessageTypes>::ServerInterface(unsigned short)", referenced from:
      CustomServer::CustomServer(unsigned short) in Server.cpp.o
  "Tachys::Networking::ServerInterface<CustomMessageTypes>::~ServerInterface()", referenced from:
      CustomServer::~CustomServer() in Server.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Source/Main/exe_server] Error 1
make[1]: *** [Source/Main/CMakeFiles/exe_server.dir/all] Error 2
make: *** [all] Error 2

但是我在CMakeList.txt中使用了add_executable

- Source
---- Main
-------- CMakeLists.txt

target_link_libraries在主要CMakeLists.txt在:

- CMakeLists.txt

似乎这些是创建可执行文件并将其链接到创建的库所需的仅有的两个函数,但我仍然收到此链接错误并且无法弄清楚要更改的内容。请帮忙。

【问题讨论】:

    标签: c++ cmake linker target-link-libraries


    【解决方案1】:

    您在源文件中实现了模板 ServerInterface&lt;CustomMessageTypes&gt;。要么将实现移至标头,这通常是您所做的,要么通过在源文件中显式实例化模板来提供符号 ServerInterface&lt;CustomMessageTypes&gt;。请参阅Why can templates only be implemented in the header file? 和其他无尽的在线资源。

    __Start__ 
    

    双下划线开头的标识符是保留的。您不能在代码中使用它们。

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2013-09-10
      • 2015-08-16
      • 1970-01-01
      • 2012-01-26
      • 2021-11-18
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多