C++ 类的头文件"symbol(s) not found"错误

C++ Header file "symbol(s) not found" error with classes

本文关键字:not 错误 found symbol 文件 C++      更新时间:2023-10-16

学习C++,我环顾四周,每次我似乎得到一个不起作用的不同答案时,也许我只是错过了一些东西。

我收到以下错误:

"/Applications/CLion EAP.app/Contents/bin/cmake/bin/cmake" --build /Users/*/Library/Caches/clion10/cmake/generated/d7f7e267/d7f7e267/Debug --target hench_modules -- -j 8
Scanning dependencies of target hench_modules
[100%] Building CXX object CMakeFiles/hench_modules.dir/main.cpp.o
Linking CXX executable hench_modules
Undefined symbols for architecture x86_64:
"Console::log(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.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[3]: *** [hench_modules] Error 1
make[2]: *** [CMakeFiles/hench_modules.dir/all] Error 2
make[1]: *** [CMakeFiles/hench_modules.dir/rule] Error 2
make: *** [hench_modules] Error 2

我正在使用 JetBrains IDE"CLion"

下面是我的代码:

主.cpp:

//in main.cpp
#include "Console/console.h"
int main() {
    Console a; // no longer produces an error, because MyClass is defined
    a.log("Hello World!");
}

console.h:

#include <string>
class Console {
    public:
        void log(std::string str);
};

控制台.cpp:

#include "console.h"
#include <iostream>
using namespace std;
void Console::log(string str){
    cout << str << endl;
};

感谢任何帮助,错误仅在实际调用a.log();时出现,在此之前没有问题。如您所见,代码非常简单,只需遵循通用指南即可。

我的问题似乎与 CMake 有关,而且非常简单/愚蠢。

CMakeLists.txt文件的更改:

set(SOURCE_FILES
    Console/console.cpp
    Console/console.h
    main.cpp)

所以正如WhozCraig指出的那样,文件被链接到代码并被找到,但实际上并没有构建。

相关文章: