2015-01-04 164 views
0

学习C++,我环顾四周,每次我似乎得到一个不起作用的答案,也许我只是缺少一些东西。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 “克利翁”

下面是我的代码:

main.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); 
}; 

console.cpp:

#include "console.h" 
#include <iostream> 
using namespace std; 

void Console::log(string str){ 
    cout << str << endl; 
}; 

任何帮助表示赞赏,误差居然叫a.log();的时候才出现,在此之前,没有任何问题。正如你所看到的,代码非常简单,只需遵循一般指南。

+0

它会出现'console.cpp'的对象代码永远不会与您的程序链接。不管这是因为它首先没有被建立,还是仅仅被排除在链接步骤之外还不清楚。 – WhozCraig 2015-01-04 13:15:14

+0

尝试'#include“console.h”'而不是'main.cpp'。 – Hayden 2015-01-04 13:35:30

+0

抱歉Hayden,console.h在文件夹“Console”中,因此更改引用会因为其他原因而中断代码 – TheHench 2015-01-05 19:05:50

回答

2

我的问题似乎与CMake,并非常简单/愚蠢。

更改到CMakeLists.txt文件:

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

所以,这是因为WhozCraig指出,该文件被链接到和发现的代码,但没有真正建立。