使用CMAKE生成makefile后的未定义引用

时间:2017-10-24 21:25:55

标签: c++ makefile cmake

我可以生成我的CMake项目,但我无法构建它。

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

├── 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文件:

# 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 ..时,它可以正常工作。

但是,当我使用make时,我会遇到很多关于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"       

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

1 个答案:

答案 0 :(得分:1)

您的src/project/CMakeLists.txt似乎忽略了将其他cpp文件(例如udp_server.cppudp_client.cpp)指定为可执行文件cpp_sockets的依赖项。你需要这样做才能让CMake编译那些源文件;因为你已经包含了相应的标题,它不会自动计算出来进行编译。事实上,add_executable声明不需要对标题说任何内容,因为include_directories命令会处理这个问题。

鉴于main.cpp所依赖的标头文件,其add_executable行应如下所示:

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