I am trying to compile and run the following simple code in MAC OS X 10.9 with CLANG++:
#include "ACE_wrappers/ace/MEM_Stream.h"
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
ACE_MEM_Stream m(3);
m.close_reader();
return 0;
}
But I get the following error:
QAs-MacBook:aceTestLinking Moamen$ clang++ myTest.cpp
Undefined symbols for architecture x86_64:
"ace_os_main_i(int, char**)", referenced from:
_main in moamen-511a28.o
"ACE_SOCK::ACE_SOCK()", referenced from:
ACE_MEM_IO::ACE_MEM_IO() in moamen-511a28.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The compile line:
clang++ myTest.cpp
The CLANG Compiler version I am using:
QAs-MacBook:aceTestLinking Moamen$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Notice - I have also tried to compile with "-std=c++11", "-stdlib=libstdc++", as in the following topic:
C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1
but did not solve the issue.
Thanks,
Moamen
Thanks for the replies, after some investigation on Linux Ubuntu, I succeeded to compile the code with the following command g++ -o m.out myTest.cpp ACE_wrappers/lib/libACE.a -I ACE_wrappers/ -lpthread -ldl. Seems some default libs were missing!
Related
I'm trying to play around with the new shared_timed_mutex structures from C++ 14 on OS X 10.11.2 using Eclipse CDT 4.5.0. Xcode is 7.2. I'm using GCC C++ with the following options:
-O0 -g3 -Wall -c -fmessage-length=0 -std=c++14
Her's what I get:
Invoking: MacOS X C++ Linker
g++ -o "MyProject" ./src/main.o
Undefined symbols for architecture x86_64:
"std::__1::shared_timed_mutex::shared_timed_mutex()", referenced from:
HashMap<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<int> >::HashMap() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here's my clang version info:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
I've searched my way through the web and found a couple of recommendations, for example this one on Reddit:
https://www.reddit.com/r/cpp_questions/comments/3ejfkr/is_c14s_stdshared_timed_mutex_available_on_os_x/?
This is very close to my problem, I've tried the suggestions, but I´m hoping, that I won´t have to do the manual libc++ download, but could stick with the standard libs shipped with my system.
Thank you!
For the past two years (as I write this) Apple has updated the libc++ headers for their tools releases, but not the libc++ sources. And libc++ implements std::shared_time_mutex both in <shared_mutex> and in shared_mutex.cpp.
According to Apple dev presentation the shared_timed_mutex will be available starting with macOS 10.12. See page 52
Yak-shaving alert.
Although I am precluded from displaying any source code, I figure with a well-written post I may be able to provide enough info to get assistance. The steps I have tried below have all been garnered from other posts, and it's becoming a bit circular now.
I'm on OS X with the following:
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ which g++
/usr/bin/g++
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ arch
i386
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ brew --config
HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: edcf1d119c4ca9d79d7147a684b7d74767cbb1f6
Last commit: 6 weeks ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
CPU: dual-core 64-bit penryn
OS X: 10.9.5-x86_64
Xcode: N/A
CLT: 6.2.0.0.1.1424975374
Clang: 6.0 build 600
X11: 2.7.7 => /opt/X11
System Ruby: 2.0.0-p481
Perl: /usr/bin/perl
Python: /Library/Frameworks/Python.framework/Versions/2.7/bin/python => /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
Java: 1.6.0_65-b14-468
So I am given three files:
Metaphone3.cpp
Metaphone3ExampleCode.cpp
Metaphone3.h
I try to compile with g++:
g++ Metaphone3.cpp
I get:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gcc and clang++ report the same.
Adding -m32 has no affect.
g++ Metaphone3.cpp -I /usr/local/include has no effect
If I try:
g++ -Wall -c Metaphone3.cpp
This gets rid of the warning, and a Metaphone3.o and Metaphone get generated.
If I try to execute:
MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ ./Metaphone
-bash: ./Metaphone: Malformed Mach-o file
Some more research indicates that I may have missed a linking step. So:
gcc Metaphone3.o -o Metaphone3
But this brings me back to the original error.
Other posts then suggest dropping the -c flag, but it is this very flag that enabled me to get passed the error. So you can see how this is getting circular. As you may be gathering by now, I am a developer, but not a C++ developer, and coming from Python, the compilation world is a new one to me. Any and all assistance appreciated
An educated wild guess: main is in Metaphone3ExampleCode.cpp. You need to compile both, and link the resulting objects together.
Try
g++ -c Metaphone3.cpp
g++ -c Metaphone3ExampleCode.cpp
g++ -o Methaphone Metaphone3.o Metaphone3ExampleCode.o
or
g++ -o Methaphone Metaphone3.cpp Metaphone3ExampleCode.cpp
metaphone3.cpp should be compiled to an .so - it's a library and not an application
the example code is provided as a guide and is not intended to be compiled
if you make metaphone3.so, you'll need to make a c++ application yourself to link to it and test it
In case anyone else stumbles upon this in a google search, I received the same error above because of a simple typo: maint() instead of main()
smh
I am trying to compile this example program
#include <iostream>
#include <gmpxx.h>
int main(int argc, const char * argv[])
{
mpz_class rho = 3;
std:cout << rho;
return 0;
}
and I get this error:
Undefined symbols for architecture x86_64:
"___gmpz_clear", referenced from:
__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr() in main-5858d5.o
"___gmpz_init_set_si", referenced from:
__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::init_si(long) in main-5858d5.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 compile my main manually with this command:
clang++ -L /opt/local/lib -I /opt/local/include -lgmpxx main.cpp
GMP has been installed through MacPorts:
$port list gmp
gmp #6.0.0 devel/gmp
Compiler's info:
$clang++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
some more info:
ll /opt/local/lib/libgmp*
/opt/local/lib/libgmp.10.dylib
/opt/local/lib/libgmp.a
/opt/local/lib/libgmp.dylib -> libgmp.10.dylib
/opt/local/lib/libgmpxx.4.dylib
/opt/local/lib/libgmpxx.a
/opt/local/lib/libgmpxx.dylib -> libgmpxx.4.dylib
and:
ll /opt/local/include/gmp*
/opt/local/include/gmp.h
/opt/local/include/gmpxx.h
Running clang++ -L /opt/local/lib -I /opt/local/include -lgmpxx -lgmp main.cpp made the trick thanks #n.m.
According to ld man pages, the -x link flag suppresses putting non-global symbols into the output file's symbol table. These symbols are useful for debugging but are not used at runtime. But this flag is causing link errors for me on Mavericks. For example, the following source file:
struct Yo
{
Yo() {}
};
void useYo()
{
Yo yo;
}
Compiled/linked as follows:
c++ -arch x86_64 -bundle -Wl,-x -o tc.so tc.cpp
Produces the following output:
ld: internal error: atom not found in symbolIndex(__ZN2YoC1Ev) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The constructor Yo::Yo() is the problem:
c++filt __ZN2YoC1Ev
Yo::Yo()
Removing the -x link flag fixes the problem. Moving the constructor implementation outside the structure declaration also fixes the problem. This code compile/links fine:
struct Yo
{
Yo();
};
Yo::Yo() {}
void useYo()
{
Yo yo;
}
Here's my compiler info:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
Is this a bug in clang or the linker, or is there some reason I should not be using the -x link flag?
So I've spent the last 20 hours trying to get boost working under OS X 10.8, and I have finally gotten it to compile without errors, but when I try to compile a test case that uses Boost.test, I'm back again in a world of hurt.
I should mention, the reason I compile boost myself instead of using the binary available, is because I want to use c++11 and libc++.
When I compiled boost, I called b2 like this:
./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-std=c++11 -stdlib=libc++" link=static
and it compiles all file. Then I have this piece of code
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Addition
#include <boost/test/unit_test.hpp>
int addition(int i, int j)
{
return i + j;
}
BOOST_AUTO_TEST_CASE(universeInOrder)
{
BOOST_CHECK(addition(2, 2) == 4);
}
which I
try to compile with
clang++ -std=c++11 -stdlib=libc++ -g -Wall -v -I/Users/cb/Downloads/boost_1_51_0 tests/arithmetic.cpp -o tests/arithmetic /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a`
And it fails miserably, giving me this error:
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o tests/arithmetic /var/folders/pg/4wcxn1j12c3188vqrv0x4w9r0000gn/T/arithmetic-UFmO1B.o /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a -lc++ -lSystem /usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
_main in arithmetic-UFmO1B.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
At first, I figured it was because boost was not compiled for 64bit so I tried telling b2 to do that specifically, but it made no difference, and I also think that it compiles for 64bit by default on OS X.
Any ideas as to why it is failing and how I get it working?
It looks like bad compilation of the test. You requested
#define BOOST_TEST_DYN_LINK
but than you are linking the static version of boost.test
/Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a
and IIRC there is a difference between the static and dynamic versions of this library. So either link the dynamic version of the library (the one with .so extension), or remove that define.