提问人:PaoDerDoktor 提问时间:3/17/2021 更新时间:3/18/2021 访问量:841
C++ / CMake / Conan.io - 包含标头时出现“对 Class::Method 的未定义引用”
C++ / CMake / Conan.io - 'Undefined Reference to Class::Method' when including header
问:
我在使用 Conan.io 和 CMake 编译项目时遇到了一些麻烦。
我正在构建一个基于 OpenGL 的小型项目。我使用 MVC 架构。我希望 CMake 产生两个不同的:.exe
main.exe
是一个具有简单 OpenGL 上下文的小型 GLFW 窗口。它构建并运行良好,使用 conan.io 来管理使用的库。pentest.exe
是一个简单的测试可执行文件,我想用它来测试我的模型中的一些基本功能。当我调用命令时,这个不会被编译。make
这是我简化的项目架构:
├───build
│ ├───.cmake
│ │
│ ├───bin
│ │ └─── // .exe files are here
│ │
│ ├───CMakeFiles
│ │ └─── // Cmake files are here
│ │
│ └─── // ...
│
├───include
│ ├───GL
│ │ └─── GLU.h
│ │
│ └───model
│ ├───Block.hpp
│ └───GameGrid.hpp
│
├───src
│ ├───model
│ │ ├───Block.hpp
│ │ └───GameGrid.hpp
│ │
│ ├───main.cpp
│ └───pentest.cpp
│
├───CMakeLists.txt
└───conanfile.txt
请注意:
pentest.cpp
不依赖于任何外部库。- 尽管我的类是一个模板类,但我使标头在末尾包含实现文件(在此 StackOverflow 问题之后)。
GameGrid
- 我对 CMake 非常非常糟糕。
- CMake 命令运行良好,当该命令调用链接器时发生错误。
make
pentest.exe
这是我的CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8.12)
project(TheEndless)
add_definitions("-std=c++17")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/model
)
link_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(main src/main.cpp)
add_executable(pentest src/pentest.cpp)
target_link_libraries(main ${CONAN_LIBS})
target_link_libraries(pentest ${CONAN_LIBS})
这是我的pentest.cpp
:
#include <iostream>
#include <string>
#include "model/Block.hpp"
#include "model/GameGrid.hpp"
int main(int argc, char const *argv[]) {
theendless::model::Block b;
theendless::model::GameGrid<1, 1> g;
g(0, 0) = b;
std::string s(g(0, 0).get_name());
std::cout << s << std::endl;
return 0;
}
这是我的模型/Block.hpp
:
#ifndef THEENDLESS_MODEL_BLOCK_HPP
#define THEENDLESS_MODEL_BLOCK_HPP
#include <string>
namespace theendless::model {
class Block {
private:
std::string name;
public:
Block();
Block(std::string name);
std::string get_name() const;
void set_name(const std::string newName);
};
}
#endif
这是我的模型/Block.cpp
:
#include "model/Block.hpp"
#include <string>
namespace theendless::model {
Block::Block() : name("default_name") {}
Block::Block(std::string name) : name(name) {}
std::string Block::get_name() const { return this->name; }
void Block::set_name(const std::string newName) { this->name = newName; }
}
以下是显示的错误:make
PS C:\projects\TheEndless\build> make
Scanning dependencies of target pentest
[ 75%] Building CXX object CMakeFiles/pentest.dir/src/pentest.cpp.obj
[100%] Linking CXX executable bin/pentest.exe
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/pentest.dir/objects.a(pentest.cpp.obj): in function `main':
C:/projects/TheEndless/src/pentest.cpp:9: undefined reference to `theendless::model::Block::Block()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/projects/TheEndless/src/pentest.cpp:13: undefined reference to `theendless::model::Block::get_name[abi:cxx1c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/pentest.dir/objects.a(pentest.cpp.obj): in function `std::array<theendless::model::Block, 1ull>::array()':
c:/mingw/include/c++/9.2.0/array:94: undefined reference to `theendless::model::Block::Block()'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/pentest.dir/build.make:107: bin/pentest.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:124: CMakeFiles/pentest.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
请注意,包括使问题消失,但我有点想让项目干净,所以我不想这样做。model/Block.cpp
pentest.cpp
附加信息:
任何帮助将不胜感激!:)
答:
我不是 CMake 或编译器专家,但以下是我理解正在发生的事情的方式。
编译器不会四处搜索任何标头或源文件,除非它被告知必须这样做。但是编译器什么时候必须搜索它们呢?
- 文件(通常是标头)包含在编译器已知的文件中。
- 该文件已显式地被编译器知道(例如,在 CMakeLists.txt 中)。
在您的例子中,编译器知道头文件,因为它位于 pentest.cpp 源文件(上面的变体 1)中。编译器是如何知道pentest.cpp的?函数内部明确指出,特定的构建目标是从此文件构建的。#include
add_executable
pentest
那么Block.cpp呢?编译器不知道,因为它既没有包含在 CMakeLists.txt 中,也没有在编译器中说明,编译器必须使用此文件。所以编译器无法知道它。
正如您已经提到的,包含 .cpp 文件不是一个好的样式。在我看来,仅包含头文件的一个巨大优势是,如果函数的实现发生变化(不是其声明,而是函数的主体),则不必在使用它的地方重新编译所有内容。您只需要重新编译一个.cpp文件。
那么解决方案是什么呢?您必须使编译器知道应该用于构建目标的所有源文件。因此,您必须将这些源文件添加到函数中。add_executable的 CMake 文档将告诉您以下内容。pentest
add_executable
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
[EXCLUDE_FROM_ALL]
[source1] [source2 ...])
添加一个可执行目标,该可执行目标被调用以从命令调用中列出的源文件生成。
因此,您必须将用于构建目标的所有源文件添加到相同的命令调用中。在您的例子中,CMakeLists.txt 将如下所示。请注意,我必须在我的机器上添加和删除才能强制使用 C++17。add_executable
target_compile_features
add_compile_definitions
cmake_minimum_required(VERSION 2.8.12)
project(TheEndless)
# add_definitions("-std=c++17") # does not work on my Windows 10 machine
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/model
)
link_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(main src/main.cpp)
# add all source files needed to build to the executable
# GameGrid.cpp is not needed because it is included in the header.
add_executable(pentest src/pentest.cpp src/model/Block.cpp)
target_link_libraries(main ${CONAN_LIBS})
target_link_libraries(pentest ${CONAN_LIBS})
target_compile_features(pentest PRIVATE cxx_std_17) # added to really use C++17, see the docs for explanation
我只看到“问题”:Visual Studio 将 Block.hpp、GameGrid.hpp 和 GameGrid.cpp 标记为“外部依赖项”。如果您希望它们显示为目标的一部分,也可以将它们添加到 .我不确定是否有其他解决方案,因为它似乎有点多余。add_executable
评论
undefined reference to 'WinMain'
add_executable(block src/model/block.cpp)
src/model/block.cpp
Block::Block()
Block