Compile error when including boost/thread.hpp - c++

I'm trying to use threads in C++, using boost's thread library. Clang is giving me an error and I don't know how to interpret it. The file I'm trying to compile is about as minimal as it can get:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
int main() {
return 0;
}
Here's the transcript:
$ clang++ -std=c++11 -o 4.4 4.4.cpp -v
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name 4.4.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/mt/k4dhhm7d7_7_q6drl3zkjj4r0000gn/T/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/thinkpad20/Documents/workspace/cpp/homework4 -ferror-limit 19 -fmessage-length 100 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/mt/k4dhhm7d7_7_q6drl3zkjj4r0000gn/T/4-49j8fE.o -x c++ 4.4.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "/usr/include/c++/4.0.0"
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/backward
/usr/bin/../lib/clang/4.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o 4.4 /var/folders/mt/k4dhhm7d7_7_q6drl3zkjj4r0000gn/T/4-49j8fE.o -lstdc++ -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
___cxx_global_var_init3 in 4-49j8fE.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init1 in 4-49j8fE.o
___cxx_global_var_init2 in 4-49j8fE.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help is appreciated! :)

You need to tell clang which libraries to link against. In your case, this is boost_system (that's what the error message tells you) and (as soon as you actually get around to using some threading stuff in your program) boost_thread as well, so compile it like this:
clang++ -std=c++11 -o myapp myfile.cpp -lboost_system -lboost_thread

Related

After update of llvm cannot find <filesystem> references on macOS with hombrew

I have updated my llvm installation, such that I now use Clang8.
After that I keep getting this error:
Undefined symbols for architecture x86_64:
"std::__1::__fs::filesystem::__status(std::__1::__fs::filesystem::path const&, std::__1::error_code*)", referenced from:
std::__1::__fs::filesystem::is_directory(std::__1::__fs::filesystem::path const&) in main-de2419.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
So it can find the header for filesystem but not the lib (as I understand). I have this test program:
#include <filesystem>
#include <iostream>
int main(void)
{
auto isDir = std::filesystem::is_directory("./main.cpp");
std::cout << isDir << std::endl;
return 0;
}
And I compile it this way clang++ -std=c++17 main.cpp. If with -v the output is:
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
"/usr/local/Cellar/llvm/8.0.0/bin/clang-8" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 409.12 -v -resource-dir /usr/local/Cellar/llvm/8.0.0/lib/clang/8.0.0 -stdlib=libc++ -internal-isystem /usr/local/opt/llvm/bin/../include/c++/v1 -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /Users/larsnielsen/Desktop/test/test_filesystem -ferror-limit 19 -fmessage-length 160 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/df/wm8q24k53dz09jn4q35122bw0000gn/T/main-2be1ed.o -x c++ main.cpp
clang -cc1 version 8.0.0 based upon LLVM 8.0.0 default target x86_64-apple-darwin18.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/opt/llvm/bin/../include/c++/v1
/usr/local/include
/usr/local/Cellar/llvm/8.0.0/lib/clang/8.0.0/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -lto_library /usr/local/Cellar/llvm/8.0.0/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -o a.out /var/folders/df/wm8q24k53dz09jn4q35122bw0000gn/T/main-2be1ed.o -lc++ -lSystem /usr/local/Cellar/llvm/8.0.0/lib/clang/8.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"std::__1::__fs::filesystem::__status(std::__1::__fs::filesystem::path const&, std::__1::error_code*)", referenced from:
std::__1::__fs::filesystem::is_directory(std::__1::__fs::filesystem::path const&) in main-2be1ed.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
I have trieded a few solution options, without success:
I have changed to use <experimental/filesystem>
I have tried to link stdc++fs and c++fs
I have tried to export LDFLAGS="-L/usr/local/opt/llvm/lib" and CPPFLAGS="-I/usr/local/opt/llvm/include"
And I have added this to my .bash_profile export PATH="/usr/local/opt/llvm/bin:$PATH"
I have update LLVM on both my Fedora and macOS, and only the Mac has the issue. I updated using homebrew on macOS.

C++ Codeblocks Compilation Issue with multiple files OSX 10.11

I'm able to run single file programs but when it comes to multiple files, it will not run. Below is the simple code example and error. I have already tried changing to LLVM Clang from the default GNU GCC compiler but neither seem to work.
I've also already installed Command Line Tools and XCode as I was warned you would need this to have codeblocks working correctly.
This is the main.cpp, Sally.h and Sally.cpp file:
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally s;
return 0;
}
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally();
protected:
private:
};
#endif // SALLY_H
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{
cout << "wuttup" << endl;
}
The error message I'm getting is this:
-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -g -I -c /Users/shackletom/Desktop/Test/main.cpp -o obj/Debug/main.o
Undefined symbols for architecture x86_64:
"Sally::Sally()", referenced from:
_main in main-9bf1fd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Edit, apologies, here's the full build log:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/shackletom/Desktop/Test -ferror-limit 19 -fmessage-length 179 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/yf/ky2mbh0x0b71x9gdz0g8sp940000gn/T/main-885784.o -x c++ main.cpp
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin15.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/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.11.0 -o test /var/folders/yf/ky2mbh0x0b71x9gdz0g8sp940000gn/T/main-885784.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"Sally::Sally()", referenced from:
_main in main-885784.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Update:
Turns out that while creating the other classes I fixed this problem by making sure "Add paths to project" under File Policy was unticked.

Clang linking error on Mac OSX

I have a very basic C++ program which I cannot compile using g++ on Mac OSX 10.9.3.
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() {
};
To compile it I use g++
g++ main.cpp -o out
Here is what I get as a result :
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)
So I then run g++ main.cpp -o out -v and get this :
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)
I get stuck here.
You don't compile MyClass.cpp. Try this:
g++ main.cpp MyClass.cpp -o out

Compiling CGAL programs on mac, installed using macports

I am working on a project that requires CGAL running on a mac and am very new to C++ in general. I installed CGAL using macports using the command
sudo port install cgal
The installation completed and the .h files were placed in
/opt/local/include/cgal
I copied the tutorial program into a .cpp file and compiled using
g++ -lcgal -I/opt/local/include/ vec.cpp
This returns the error
ld: library not found for -lcgal
clang: error: linker command failed with exit code 1 (use -v to see invocation)
g++ -lcgal -v -I/opt/local/include/ vec.cpp returns
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 vec.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 -I /opt/local/include -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/sujeath/C practice -ferror-limit 19 -fmessage-length 80 -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/tm/bgg4k0xs4v36pzwxvsrqtsb00000gn/T/vec-0a56fb.o -x c++ vec.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:
/opt/local/include
/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 a.out /var/folders/tm/bgg4k0xs4v36pzwxvsrqtsb00000gn/T/vec-0a56fb.o -lcgal -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
ld: library not found for -lcgal
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am using Mavericks BTW and gnu gcc
Thanks!
so, first of all, I'm new on stackoverflow and actually would have preferred to add a comment rather then posting an answer as I'm pretty new to the C++/Mac world too.
Stated this disclaimer I would like to point out a two things that I saw "parsing" the output you posted:
You're calling g++, however, you're using clang to compile (default on OS X Mavericks) as /usr/bin/g++ points to /usr/bin/clang++
I think the cgal library is not on your search path. Marc Glisse pointed you to the -L option of the ld command that let you define further search directories. Probably -L/opt/local/lib might work for you using Macports (I didn't use Macports for a long time so I'm not sure if that's the correct path for the libraries).
Hope this helps.

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

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
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?
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
You missed other cpp file:
clang++ main.cpp test.cpp -o main