Why won’t this simple C++ code compile with clang++?

user3785066

Here is a very simple C++ program.

// test.h
class Test {
     public:
         Test();
};

// test.cpp
#include "test.h"
Test::Test()
{
   // do something
}

// main.cpp
#include "test.h"
int main() {
    Test t;
    return 0;
}

I am trying to compile this using clang++ on OSX using this command line and I get this error:

[test]$ clang++ main.cpp -o main
Undefined symbols for architecture x86_64:
  "Test::Test()", referenced from:
      _main in main-017149.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And here is the compilation with the -v switch:

[test]$ clang++ main.cpp -o main -v
Apple LLVM version 6.0 (clang-600.0.34.4) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
 "/Applications/Xcode6-Beta2.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 241 -v -resource-dir /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/ramin/projects/algorithm_examples/test -ferror-limit 19 -fmessage-length 139 -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/z5/hyt30v7d7t7_w1xfpdgqp05w0000gn/T/main-d17898.o -x c++ main.cpp
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin13.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /usr/local/include
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o main /var/folders/z5/hyt30v7d7t7_w1xfpdgqp05w0000gn/T/main-d17898.o -lc++ -lSystem /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "Test::Test()", referenced from:
      _main in main-d17898.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What am I doing wrong?

Ry-

main.cpp includes test.h; it does not include test.cpp. You can compile and link them at the same time by specifying them both as input:

[test]$ clang++ main.cpp test.cpp -o main

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Simple std::regex_search() code won't compile with Apple clang++ -std=c++14

Why does VC++ compile the code while clang doesn't?

why doesn't clang++ compile the following code?

Why this code won't compile if destructor is declared?

Why won't this generic java code compile?

Why won't the following Agda code compile?

Why won't this code with Java generics compile?

Why does this clang code fail to compile with clang 10 with -std=c++20

Why does this code compile with MSVC, but not in GCC or Clang?

Why it takes me a long time to compile this simple C++ code

Why won't g++ compile a simple GTK program?

Simple C++11 hash function won't compile

Clang won't compile a template specialization that gcc will

My C++ program won't compile on Visual Studio code

Why won't this template function compile C++

The C compiler "/usr/bin/clang" is not able to compile a simple test program

Why does clang still need libgcc.a to compile my code?

Why the below code does not compile with gcc but compiles fine with clang

Why this code fails to compile with gcc 4.8.5 while it compiles fine with clang

Why does the following code compile using clang but not gcc

Code with generics won't compile

Haskell code won't compile

Why doesn't C# compile directly to machine code?

Why the code doesn't compile

Compile simple C code in Visual Studio 2013

How to compile and run a simple C code for Android?

Why does this Java code using multiple constructors run in VS code but won't compile with javac?

Very simple practice program won't compile

Clang doesn't compile code but gcc and msvc compiled it