我有一个非常基本的C ++程序,我无法在Mac OSX 10.9.3上使用g++
进行编译。
Main.cpp的
#include "MyClass.hpp"
int main() {
MyClass::MyClass obj = MyClass::MyClass();
return 0;
}
MyClass.hpp
class MyClass {
public:
MyClass();
void method();
private:
};
MyClass.cpp
#include "MyClass.hpp"
using namespace MyClass;
MyClass() {
};
void method() {
};
要编译它我使用g++
g++ main.cpp -o out
以下是我得到的结果:
Undefined symbols for architecture x86_64:
"MyClass::MyClass()", referenced from:
_main in main-2750ac.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
然后我运行g++ main.cpp -o out -v
并获取此信息:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/stan/Documents/Info/Ateliers C++/Ateliers/Drawing -ferror-limit 19 -fmessage-length 120 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/58/ch14y2yx05v7vy1yvrsmy_3m0000gn/T/main-4d20fb.o -x c++ issue/main.cpp
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o out /var/folders/58/ch14y2yx05v7vy1yvrsmy_3m0000gn/T/main-4d20fb.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"MyClass::MyClass()", referenced from:
_main in main-4d20fb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我被困在这里。
答案 0 :(得分:2)
你不能编译MyClass.cpp
。试试这个:
g++ main.cpp MyClass.cpp -o out