Trying to compile C++ code on mac - c++

I'm trying to compile some C++ code on my mac. I can run it using Xcode, but when I try to compile from the terminal using gcc I get the following error:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccOJDOlb.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccOJDOlb.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccOJDOlb.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccOJDOlb.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Anyone know what might be going on?

ld: symbol(s) not found for architecture x86_64
This is saying that 'ld', the linker, is not able to find definitions for some symbols your program uses. So you know compilation has succeeded.
And ld lists the specific symbols. Notice that they are all from the standard library. This should tell you that the standard library is probably not being found by the linker.
Typically when you use a library you have to tell the compiler toolchain using a link flag. But for a language's standard library the compilers in the GNU compiler collection generally just assume you want the library for that language. So running the gcc command automatically includes the C standard libraries, running g++ automatically includes the C++ standard libraries, etc. But notice, running 'gcc' does not automatically link in the C++ standard library. It's very likely that you're running the command 'gcc' and simply not adding the correct linker flag for the C++ standard library.
If for some reason you want to use gcc and not g++ you'll have to explicitly state that you want the standard library, using a flag like -lstdc++.
Also, unless you really want gcc and you're installing the latest versions of it yourself on OS X you may want to switch over to clang/clang++ and the libc++ implementation of the C++ standard library. The gcc that comes with Xcode is based on an old version, and the standard library is similarly old. Xcode has been transitioning to clang as the system compiler for some time now. The clang compiler driver has basic compatibility with gcc so you use many of the same basic flags. Here's a good set to start with:
clang++ -std=c++11 -stdlib=libc++ -Wall -Wextra

I try to compile from the terminal using gcc
Then try g++ instead of plain ol' gcc. The former automatically links against the C++ standard library, the latter does not.

Related

Problems compiling windows c++ project on g++ 5 [duplicate]

This question already has answers here:
Using G++ to compile multiple .cpp and .h files
(13 answers)
Closed 7 years ago.
I have trouble with my C++ project which I created and compiled successfully within Visual Studio 2013 on a Windows machine. Currently I'm migrating the source to Unix platforms (my issue had been confirmed on Mac and CentOs). My project depends on the OpenMP (multithread support) library. Hence Apples LLVM compiler (version 6.0) does not support OpenMp sufficiently I decided to compile my project using g++-5.
After handling some syntax errors when moving from windows compiler to GNU's gcc/g++ (Homebrew gcc 5.2.0) I'm experiencing the following issue in the linking process:
The linker can not find symbols for my architecture and lists nearly every class/object which is created and included by me - what's curious, it does not list every of my classes/object.
I'm compiling with this command:
g++-5 myProject.cpp -o myProject -fopenmp -std=c++11 -L/usr/local/lib -I/usr/local/Cellar/libiomp/20150701/include/libiomp -Wall
Am I missing something stupid in my compiler call? Whats the reason for my linker issues?
As mentioned above, the same issue occurs on Mac and CentOs too.
EDIT (according to some comments):
According to this question I've activated all warning flags and the compiler is definitely happy with my code. But the linker is saying the following:
Undefined symbols for architecture x86_64:
"FileHelper::createFileName[abi:cxx11](char const*, char const*)", referenced from:
_main in ccZAvFqT.o
"Statistics::writeStats(char const*)", referenced from:
_main in ccZAvFqT.o
"Statistics::newSimulationRun()", referenced from:
_main in ccZAvFqT.o
"Statistics::newSimulationIteration()", referenced from:
_main in ccZAvFqT.o
"Statistics::Instance()", referenced from:
_main in ccZAvFqT.o
"Statistics::writeAvg()", referenced from:
_main in ccZAvFqT.o
"Statistics::~Statistics()", referenced from:
_main in ccZAvFqT.o
"GraphHelper::Graph::writeGraph(std::vector<IPeer*, std::allocator<IPeer*> >*, std::vector<Connection*, std::allocator<Connection*> >*)", referenced from:
[...] and so on and so on...
When you are compiling from command line, you need to make sure you include all object files (.o) when you link the executable.
Obviously, it is hard to control this manually, this is why people invented various sorts of Makefiles to do this for them. You can research this topic. To help your immediate problem, just make sure you have all your cpp files listed in your command line for compilation. Something like
g++ myProject.cpp Statistics.cpp ... <flags> -o <executable>

Issue compiling Go program when linking to a compiled version of zeromq on Mac

I've compiled ZeroMQ on Mac OSX 10.9.5 in order to then link to a simple Go program using github.com/pebbe/zmq4, but I'm getting this error I don't understand or know exactly how to remedy.
# github.com/pebbe/zmq4
ld: warning: ignoring file /usr/local/lib/libzmq.dylib, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/lib/libzmq.dylib
Undefined symbols for architecture i386:
"_zmq_bind", referenced from:
__cgo_59814aec404b_C2func_zmq_bind in zmq4.cgo2.o
__cgo_59814aec404b_Cfunc_zmq_bind in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_Cfunc_zmq_bind, __cgo_59814aec404b_C2func_zmq_bind )
"_zmq_close", referenced from:
__cgo_59814aec404b_C2func_zmq_close in zmq4.cgo2.o
__cgo_59814aec404b_Cfunc_zmq_close in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_C2func_zmq_close,
...
"_zmq_z85_encode", referenced from:
__cgo_59814aec404b_Cfunc_zmq_z85_encode in zmq4.cgo2.o
(maybe you meant: __cgo_59814aec404b_Cfunc_zmq_z85_encode)
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status
I think it makes sense that the dylib is made for x86_64 (the Mac) but why Go is trying to link with i386? How do I control that and make it link differently? or even if it's correct?
If you build libzmq yourself, configure with --disable-shared --enable-static keys. If you use homebrew, uninstall zmq and build the library manually. If you need "fat" universal static library use this script:
https://github.com/drewcrawford/libzmq-ios

Luabind Undefined Symbols/ Luabind::scope::scope

I have just started working with Luabind, and have attempted to run the Hello World test specified at http://www.rasterbar.com/products/luabind/docs.html#calling-lua-functions. However, this provides an undefined symbol error when attempted to compile.
Undefined symbols for architecture x86_64:
"luabind::scope::scope(std::__1::auto_ptr<luabind::detail::registration>)", referenced from:
luabind::scope luabind::def<void (*)(), luabind::detail::null_type>(char const*, void (*)(), luabind::detail::null_type const&) in TestClass.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Other functions such as luabind::open and luabind::call_function work correctly.
I installed both luabind and lua 5.1 through homebrew on osx.
It looks to me like you are compiling your program against libc++ and trying to link to a luabind library that was compiled against the stdlibc++ library.
The clue is std::__1::auto_ptr. This __1 inline namespace is used by libc++ to differentiate it's ABI from stdlibc++
Thus if
luabind::scope::scope(std::__1::auto_ptr<luabind::detail::registration>)
Cannot be found it is likely because libluabind doesn't have it. Dumping it's exported symbols you might find it has
luabind::scope::scope(std::auto_ptr<luabind::detail::registration>)
Instead.
If I am right, simply recompile libluabind targeting libc++ and you should find it works with your test program.

OSX Disk Arbitration linker errors

Trying to compile my C++ application using clang which works but I get the following linker error:
Undefined symbols for architecture x86_64:
"_DADiskCopyDescription", referenced from:
Security::getHddID() in Security.cpp.o
"_DADiskCreateFromBSDName", referenced from:
Security::getHddID() in Security.cpp.o
"_DASessionCreate", referenced from:
Security::getHddID() in Security.cpp.o
ld: symbol(s) not found for architecture x86_64
I've checked the official documentation for Disk Arbitration on the Apple website but that's awful, since it doesn't even tell you which library file to include, let alone what to link to.
I'm using CMake to set up my build chain.
You need to use -framework DiskArbitration in your linker arguments.

Getting Boost asio to work

I am trying to use the Boost asio library to for sockets.
I installed boost using homebrew brew install boost
After it was built I tried the tutorial for creating a server on boost's website.
All I did was paste that code into a file called main.cc
When I try to compile g++ main.cc I get this error:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccTbzxpk.o
boost::asio::error::get_system_category() in ccTbzxpk.o
boost::system::error_code::error_code()in ccTbzxpk.o
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccTbzxpk.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
What is going wrong?
Those are linker errors. If you read the docs:
The following libraries must be available in order to link programs
that use Boost.Asio:
Boost.System for the boost::system::error_code and
boost::system::system_error classes.
Boost.Regex (optional) if you use
any of the read_until() or async_read_until() overloads that take a
boost::regex parameter.
OpenSSL (optional) if you use Boost.Asio's SSL
support.
Furthermore, some of the examples also require the Boost.Thread,
Boost.Date_Time or Boost.Serialization libraries.
Now, the errors you posted all say: boost::system, this means you need to link like this (assuming everything is the default):
g++ main.cc -lboost_system
Please read What do 'statically linked' and 'dynamically linked' mean? for in-depth information about linking.