简体   繁体   中英

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

Learning C++, I've looked around quite a bit and every time I seem to get a different answer that doesn't work, perhaps I'm just missing something.

I receive the following error:

"/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

I'm using the JetBrains IDE "CLion"

Below is my code:

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;
};

Any help is appreciated, the error only appears when actually calling a.log(); , prior to that there are no issues. As you can see the code is pretty simple and just following along a generic guide.

My issue seems to be with CMake, and very simple/silly.

Changes to the CMakeLists.txt file:

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

So the reason is as WhozCraig pointed out, the files were linked to and found by the code, but not actually built.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM