C++ 类的头文件"symbol(s) not found"错误
C++ Header file "symbol(s) not found" error with classes
学习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
指出的那样,文件被链接到代码并被找到,但实际上并没有构建。
相关文章:
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 方法错误"not all control paths return a value"和方法不返回值
- 将系数存储在头文件的数组中("does not name a type"错误)
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 结构中的错误"Incomplete type is not allowed"
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何摆脱导入的 make 项目中的 Eclipse 索引器"Type std::... could not be resolved"错误
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- 如何解决"cout was not declared in this scope"错误?
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 为什么我在" 'X' identifier not found' "上收到错误
- 如何修复此错误"the value of 'x1' is not usable in a constant expression"?static_assert
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 为什么我会收到"getuid was not declared in that scope"错误?
- 巴泽尔中的错误"name 'new_local_repository' is not defined"