The following program fails to link with clang and -stdlib=libstdc++:
$ cat future.cpp
#include <iostream>
#include <future>
int main()
{
std::future<int> f1 = std::async([](){ return 42; });
f1.wait();
std::cout << "Magic number is: " << f1.get() << std::endl;
}
$ g++-mp-5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
$ clang++-mp=3.5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
When building with clang and -stdlib=libstdc++, the following linking error occurs:
$ clang++-mp-3.5 future.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out
Undefined symbols for architecture x86_64:
"std::__once_call", referenced from:
void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
"std::__once_callable", referenced from:
void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> (std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> >() in future-b6480b.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in future-b6480b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, a simple program w/o future builds just fine, e.g.:
$ cat simple.cpp
#include <iostream>
#include <future>
int main()
{
std::cout << "Magic number is: " << 42 << std::endl;
}
$ clang++-mp-3.5 simple.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out
Magic number is: 42
System is OSX 10.10.4 with macports.
I can't figure out what is the problem. Thanks!
This is an incompatibility between GCC and Clang on Mac OS X.
GCC emits a reference to ___emutls_v._ZSt15__once_callable while clang emits a reference to __ZSt15__once_callable.
Unfortunately, __ZSt15__once_callable and ___emutls_v._ZSt15__once_callable
are not compatible, so doing something like:
asm("__ZSt15__once_callable: jmp ___emutls_v._ZSt15__once_callable");
wouldn't work either.
I also came accross this LLVM bug report: http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html which probably means clang will never add for support GCC's emutls implementation.
Edit: It looks like support for emutls was added to clang trunk a couple of hours ago in r243438 via -femulated-tls.
Clang on OSX uses an older version of libstdc++. I believe its version 4.2 which does not support many of the C++11 features. If you want to use C++11 with clang on OSX your only choice is libc++.
You could also use gcc-4.x from homebrew which will include a newer version of libstdc++. Unfortunately clang will not easily link with this version of the standard library.
Undefined symbol std::__once_call when building GCC's standard library is explained here https://github.com/msys2/MINGW-packages/issues/5786 and should be fixed when using compiler flag -femulated-tls.
Related
My question is in regards to using OpenSSL on Mac via GCC.
#include <stdio.h>
#include <openssl/rand.h>
int main()
{
unsigned char key[128];
Rand_bytes(key,128);
return 0;
}
I have the following code, that I am trying to compile with GCC. Here is what I enter into the command line
gcc -o ossl ossl.c -lcrypto -lssl
However I get the following error.
Undefined symbols for architecture x86_64:
"_Rand_bytes", referenced from:
_main in cc2hf0Ij.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am not experienced when it comes to using openssl. Why am I receiving Undefined symbols for architecture x86_64?
int main()
{
unsigned char key[128];
Rand_bytes(key,128);
return 0;
}
Try RAND_bytes:
int main()
{
unsigned char key[128];
int rc = RAND_bytes(key,sizeof(key));
if(rc != 1)
/* Handle failure */
...
OPENSSL_cleanse(key,sizeof(key));
return 0;
}
The OpenSSL docs are at RAND_bytes(3).
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I'm having a weird problem (at least in my eyes) with C++. I've created a thread pool class, and in the main function I just pushes one element to the task queue.
This gives me an error saying "undefined symbols". When the specific line is commented out, the program compiles.
Here is the push method in question:
template<class F, class ...Args>
void concurrency::thread_pool::push(F&& f, Args&&... args) {
if (stop) {
throw std::runtime_error("push on stopped thread_pool");
}
std::function<void()> func = std::bind(f, args...);
lockGuard l(mutex);
bool should_wake = queue.empty();
queue.push(func);
if (should_wake) {
cond.notify_one();
}
}
And here's the main function:
void hello_world() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
pool = new concurrency::thread_pool(5);
pool->push(hello_world); // <-- Compile error!
}
This is the command:
david$ g++ -g -Wall -std=c++0x -I ./ *.cpp
Undefined symbols for architecture x86_64:
"void concurrency::thread_pool::push<void (&)()>(void (&)())", referenced from:
_main in ccuKcG16.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
And my g++ version:
davids-mbp:cpp-thread-pool david$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc47/4.7.3/libexec/gcc/x86_64-apple-darwin13.0.2/4.7.3/lto-wrapper
Target: x86_64-apple-darwin13.0.2
Configured with: ../configure --build=x86_64-apple-darwin13.0.2 --prefix=/usr/local/Cellar/gcc47 /4.7.3 --enable-languages=c,c++,objc,obj-c++ --program-suffix=-4.7 --with-gmp=/usr/local/opt/gmp4 --with-mpfr=/usr/local/opt/mpfr2 --with-mpc=/usr/local/opt/libmpc08 --with-ppl=/usr/local/opt/ppl011 --with-cloog=/usr/local/opt/cloog-ppl015 --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --enable-plugin --disable-nls --disable-multilib
Thread model: posix
gcc version 4.7.3 (GCC)
Update
thread_pool.hpp: http://pastebin.com/62SDD0ZY
thread_pool.cpp: http://pastebin.com/nqz57HS9
Your problem might be reduced to
g++ linking order dependency when linking c code to c++ code
Because you use *.cpp in your command line
Update
I'm afraid your post doesn't provide enought information, but I bet your problem is
Storing C++ template function definitions in a .CPP file
Goal: I want to use thread STL of C++11 in Matlab mex file (R2013a) using Xcode 4.6
I modified ~/.matlab/R2013a/mexopts.sh
CC='clang++' # was llvm-gcc-4.2
CXX='clang++' # was llvm-g++-4.2
MACOSX_DEPLOYMENT_TARGET='10.8' # was 10.5. C++11 is supported >=10.7
CXXFLAGS="$CXXFLAGS -std=gnu++11 -stdlib=libc++" # additional flags
Normal mex files without C++11 features are compiled well. Further, STL is well detected by the compiler except linking failure.
>> mex mextest.cpp
Undefined symbols for architecture x86_64:
"std::__1::__thread_struct::__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_struct::~__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_local_data()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__throw_system_error(int, char const*)", referenced from:
_mexFunction in mextest.o
"std::__1::thread::join()", referenced from:
_mexFunction in mextest.o
"std::__1::thread::~thread()", referenced from:
_mexFunction in mextest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mex: link of ' "mextest.mexmaci64"' failed.
Error using mex (line 206)
Unable to complete successfully.
The actual source code is shown below. The details are not important because it compiles well in Matlab R2013 WINDOWS version with Visual Studio 2012 Express. An equivalent cpp was also well compiled with "clang++ -std=gnu++11 -stdlib=libc++ clangtest.cpp". So, at least, there is no logical error in the codes (I'm not saying it is safe codes. It is just a test.)
#include "mex.h"
#include <thread>
#include <stdio.h>
int count_thread1 = 0;
int count_thread2 = 0;
void hello()
{
count_thread2 = 0;
for(int i=0; i<=10000; i++){
for (int j=1;j<=20000;j++){
count_thread2 = i-j-1;
}
count_thread2++;
printf("2: %d , %d\n", count_thread1, count_thread2); // Not sure if printf is thread-safe in Matlab. But it works in this particular example
}
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
count_thread1 = 0;
std::thread t(hello);
for (int i=1;i<=10000;i++)
{
for (int j=1;j<=20000;j++){
count_thread1 = -i+j-1;
}
count_thread1++;
mexPrintf("1: %d , %d\n", count_thread1, count_thread2);
}
mexPrintf("\n");
t.join();
mexPrintf("Done\n");
}
It seems like I have to replace some include directories and/or library directories. What kind of options should be modify?
Thank you.
The error is due to compiling against -stdlib=libc++ but linking against -lstdc++. You can fix it in one of two ways:
Fix it in mexopts.sh. The most drastic and effective solution. Located in ~/.matlab/${MATLAB_VERSION}/mexopts.sh, this determines all compiler options. Simply find/replace all stdc++ to c++.
Patchwork solution: Simply add -lc++ to the tail end of CXXLIBS. I'm not sure what the effect of linking against multiple versions of the standard libraries is, but it seems to work. In your mex invocation, add the argument CXXLIBS="\$CXXLIBS -lc++".
As a secondary issue, I believe you're completely overwriting the value of CXXFLAGS; you must escape the $ symbol as I did above with the libraries.
I am writing a small app on Mac 10.7.5 with gcc47 via macports in Eclipse CDT to learn the new features in C++11. I have a large amount of code compiling, linking and running. When I add the call to "async" I get a linker error. The most simple program that will reproduce the link error follows. Thanks in advance for any help.
#include <iostream>
#include <future>
using namespace std;
void bar()
{
cout << "!!!Hello World!!!" << endl;
}
int main() {
future<void> f1(async(bar));
return 0;
}
The output is:
21:56:46 **** Incremental Build of configuration Debug for project Hello ****
make all
Building file: ../src/Hello.cpp
Invoking: GCC C++ Compiler g++ -D__GXX_EXPERIMENTAL_CXX0X__ -I/opt/local/include/gcc47/c++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Hello.d" -MT"src/Hello.d" -o "src/Hello.o" "../src/Hello.cpp"
Finished building: ../src/Hello.cpp
Building target: Hello
Invoking: MacOS X C++ Linker g++ -o "Hello"
./src/Hello.o Undefined symbols for architecture x86_64:
"___emutls_v._ZSt11__once_call", referenced from:
void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void
(std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in
Hello.o "___emutls_v._ZSt15__once_callable", referenced from:
void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void
(std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in
Hello.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&)>
(std::__future_base::_State_base*,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool>)> >() in Hello.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in
Hello.o ld: symbol(s) not found for architecture x86_64 collect2:
error: ld returned 1 exit status make: *** [Hello] Error 1
The information at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54806 and https://trac.macports.org/ticket/36093 suggests that this error is due to a bug in the MacPorts version of GCC. According to https://trac.macports.org/ticket/36093#comment:35 this has been fixed as of r98493 of MacPorts. I would expect that your problem would go away if you updated to that revision of the port repository and then reinstalled GCC.
I wrote a hello world program to see how curses library works.
Here is my program:
/Users/snihalani/dev/daas at 10:10AM
➜ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main(void)
{
int returnValue = 0;
while(1)
{
printf("I got %d\n", getch());
}
return 0;
}
I ran gcc main.c
I got
/Users/snihalani/dev/daas at 10:14AM
➜ gcc main.c
Undefined symbols for architecture x86_64:
"_stdscr", referenced from:
_main in ccEvUdhx.o
"_wgetch", referenced from:
_main in ccEvUdhx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I don't what's going wrong. Can anyone please help?
Nevermind. I had to add -lcurses option while compiling.