使用CMAKE生成Makefile后的未定义引用 [英] Undefined reference after using CMAKE to generate a makefile

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

问题描述

我可以生成我的CMake项目,但不能构建它.

I can generate my CMake project, but I can't build it.

我有一个相当简单的项目结构:

I have a fairly straightforward project structure:

├── bin                                                                                                                                                                                                                                       
├── build                                                                                                                                                                                                                                     
├── CMakeLists.txt                                                                                                                                                                                                                            
├── include                                                                                                                                                                                                                                   
│   └── project                                                                                                                                                                                                                               
│       ├── base_socket.h                                                                                                                                                                                                                     
│       ├── tcp_client.h                                                                                                                                                                                                                      
│       ├── tcp_server.h                                                                                                                                                                                                                      
│       ├── udp_client.h                                                                                                                                                                                                                      
│       └── udp_server.h                                                                                                                                                                                                                      
├── LICENSE.txt                                                                                                                                                                                                                               
├── README.md                                                                                                                                                                                                                                 
└── src                                                                                                                                                                                                                                       
    └── project                                                                                                                                                                                                                               
        ├── base_socket.cpp                                                                                                                                                                                                                   
        ├── CMakeLists.txt                                                                                                                                                                                                                    
        ├── main.cpp                                                                                                                                                                                                                          
        ├── tcp_client.cpp                                                                                                                                                                                                                    
        ├── tcp_server.cpp                                                                                                                                                                                                                    
        ├── udp_client.cpp                                                                                                                                                                                                                    
        └── udp_server.cpp 

在顶层目录中还有一个相当简单的CMakeLists.txt文件:

And a fairly straight forward CMakeLists.txt file in the top directory:

# Project initialization                                                                                                                                                                                                                      
cmake_minimum_required(VERSION 2.7.2 FATAL_ERROR)                                                                                                                                                                                             

project("cpp_sockets")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

# Add include directory                                                                                                                                                                                                                       
include_directories(include)                                                                                                                                                                                                                  

# Add subdirectories                                                                                                                                                                                                                          
add_subdirectory(src/project)                                                                                                                                                                                                                 

src/project/CmakeLists.txt 中也很简单:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)                                                                                                                                                                                   
set(HEADER_DIR ${CMAKE_SOURCE_DIR}/include/project)                                                                                                                                                                                           
set(HEADER_FILES                                                                                                                                                                                                                              
  ${HEADER_DIR}/base_socket.h                                                                                                                                                                                                                 
  ${HEADER_DIR}/tcp_client.h                                                                                                                                                                                                                  
  ${HEADER_DIR}/tcp_server.h                                                                                                                                                                                                                  
  ${HEADER_DIR}/udp_client.h                                                                                                                                                                                                                  
  ${HEADER_DIR}/udp_server.h                                                                                                                                                                                                                  
  )                           

add_executable(cpp_sockets main.cpp ${HEADER_FILES}) 

当我 cd 进入/build 并键入 cmake .. 时,它工作正常.

When I cd into /build and type cmake .. it works fine.

但是,当我跟随 make 进行操作时,我遇到许多有关 undefined reference 的错误.

However, when I follow that with make, I get many terrible errors about undefined reference.

main.cpp:(.text+0x2c6): undefined reference to `UDPClient::UDPClient(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                            
main.cpp:(.text+0x2eb): undefined reference to `UDPClient::send_message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                              
main.cpp:(.text+0x359): undefined reference to `UDPServer::UDPServer(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                            
main.cpp:(.text+0x380): undefined reference to `UDPServer::socket_bind()'                                                                                                                                                                     
main.cpp:(.text+0x38c): undefined reference to `UDPServer::listen()'                                                                                                                                                                          
main.cpp:(.text+0x400): undefined reference to `TCPClient::TCPClient(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                            
main.cpp:(.text+0x40c): undefined reference to `TCPClient::make_connection()'                                                                                                                                                                 
main.cpp:(.text+0x42b): undefined reference to `TCPClient::send_message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                              
main.cpp:(.text+0x492): undefined reference to `TCPServer::TCPServer(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'                                                                            
main.cpp:(.text+0x4b9): undefined reference to `TCPServer::socket_bind()'                                                                                                                                                                     
CMakeFiles/cpp_sockets.dir/main.cpp.o: In function `UDPClient::~UDPClient()':                                                                                                                                                                 
main.cpp:(.text._ZN9UDPClientD2Ev[_ZN9UDPClientD5Ev]+0x14): undefined reference to `Socket::~Socket()'                                                                                                                                        
CMakeFiles/cpp_sockets.dir/main.cpp.o: In function `UDPServer::~UDPServer()':                                                                                                                                                                 
main.cpp:(.text._ZN9UDPServerD2Ev[_ZN9UDPServerD5Ev]+0x14): undefined reference to `Socket::~Socket()'                                                                                                                                        
CMakeFiles/cpp_sockets.dir/main.cpp.o: In function `TCPClient::~TCPClient()':                                                                                                                                                                 
main.cpp:(.text._ZN9TCPClientD2Ev[_ZN9TCPClientD5Ev]+0x14): undefined reference to `Socket::~Socket()'                                                                                                                                        
CMakeFiles/cpp_sockets.dir/main.cpp.o: In function `TCPServer::~TCPServer()':                                                                                                                                                                 
main.cpp:(.text._ZN9TCPServerD2Ev[_ZN9TCPServerD5Ev]+0x14): undefined reference to `Socket::~Socket()'                                                                                                                                        
collect2: error: ld returned 1 exit status                                                                                                                                                                                                    
src/project/CMakeFiles/cpp_sockets.dir/build.make:94: recipe for target '../bin/cpp_sockets' failed                                                                                                                                           
make[2]: *** [../bin/cpp_sockets] Error 1                                                                                                                                                                                                     
CMakeFiles/Makefile2:85: recipe for target 'src/project/CMakeFiles/cpp_sockets.dir/all' failed                                                                                                                                                
make[1]: *** [src/project/CMakeFiles/cpp_sockets.dir/all] Error 2                                                                                                                                                                             
Makefile:83: recipe for target 'all' failed                                                                                                                                                                                                   
make: *** [all] Error 2

main.cpp 文件如下:

  #include "project/udp_server.h"                                                                                                                                                                                                            
  #include "project/udp_client.h"                                                                                                                                                                                                            
  #include "project/tcp_client.h"       

为什么链接器无法找到这样的引用?

Why would the linker not be able to find the references like this?

推荐答案

您的 src/project/CMakeLists.txt 似乎忽略了指定其他cpp文件,例如 udp_server.cpp udp_client.cpp ,作为可执行文件 cpp_sockets 的依赖项.您需要执行此操作,以使CMake编译这些源文件.它不会仅仅因为您已经包含了它们相应的头文件而自动找出要编译它们的方法.实际上, add_executable 声明不需要说明任何有关标头的信息,因为 include_directories 命令可以解决这个问题.

It looks like your src/project/CMakeLists.txt neglects to specify the other cpp files, like udp_server.cpp and udp_client.cpp, as dependencies of the executable cpp_sockets. You need to do this in order for CMake to compile those source files; it won't automatically figure out to compile them just because you have included their corresponding headers. In fact, the add_executable declaration doesn't need to say anything about the headers, because the include_directories command takes care of that.

给出您的 main.cpp 所依赖的头文件,其 add_executable 行应如下所示:

Given the header files that your main.cpp depends on, its add_executable line should look like this:

add_executable(cpp_sockets main.cpp udp_server.cpp udp_client.cpp tcp_client.cpp)

这篇关于使用CMAKE生成Makefile后的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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