matlab mex clang C++11 thread -> Undefined symbols error - xcode

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.

Related

std::future and clang with -stdlib=libstdc++

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.

gcc 4.2.1 Linking issue: ATLAS 3.8.3

I'm trying to use CBLAS ATLAS. I'm a beginner. I have the following C code.
// tmp.cpp file
#include<stdio.h>
#include<stdlib.h>
#include<cblas.h>
int main(void){
float x[2] = {6,2};
float *y = (float *)malloc(2*sizeof(float));
const int n = 2;
const int incx = 1;
const int incy = 1;
float a = 5.4;
y[0] = 4.2; y[1] = 4.5;
cblas_saxpy(n,a,x,incx,y,incy);
free(y);
}
I installed the ATLAS library following the video http://youtu.be/DvLSr6zN0pU?t=6m5s and the next video "part3". However, I have no FORTRAN compiler, so I configure with
$../ATLAS/confiugre --nof77
The installation process took place as described in the video.
iMac:Desktop sotero$ ls /usr/local/atlas/include
atlas cblas.h clapack.h
iMac:Desktop sotero$ ls /usr/local/atlas/lib/
libatlas.a libcblas.a liblapack.a libptcblas.a
I tried compiling with this result
iMac:Desktop sotero$ c++ tmp.cpp
tmp.cpp:3:18: error: cblas.h: No such file or directory
tmp.cpp: In function ‘int main()’:
tmp.cpp:14: error: ‘cblas_saxpy’ was not declared in this scope
I read how to make the link to the website http://math-atlas.sourceforge.net/errata.html#LINK and I have the following to compile
iMac:Desktop sotero$ c++ tmp.cpp -L/usr/local/atlas/lib/ -lcblas -latlas -I/usr/local/atlas/include/
Undefined symbols:
"cblas_saxpy(int, float, float const*, int, float*, int)", referenced from:
_main in ccPop12J.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I do not know how I should compile. I just need to level one BLAS. I've been looking to compile if you do link to the ATLAS library, but I'm lost.
How I can fix it?

CppUTest example doesn't work

I just installed CppUTest on my MAC using brew as indicated by the guide.
It failed when I tried to build the example cpp.
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
I guess it is because the header file which define those macros are not included. So I add include as below:
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
Now I get bunch of errors.
Undefined symbols for architecture x86_64: "UtestShell::assertTrue(bool, char const*, char const*, char const*, int)", referenced from: vtable for TEST_FirstTestGroup_FirstTest_TestShellin ccNDwnbv.o
The error you are getting is a linker error and it suggests that you are not linking the CppUTest library. It is hard to actually say what is wrong because your question is missing the Makefiles. Could you explain how you have compiled the example?

Not able to compile with curses on gcc (OS X))

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.

Xcode 'Undefined symbols for architecture x86_64:' error?

Error: " Undefined symbols for architecture x86_64:
"f(int, int, int const (*) [8], int const (*) [8], int*, int*)", referenced from:
_main 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)
I tried running the exact same code on Visual Studio 2010, and it worked! Any idea why it doesn't work here? My Mac is 64bit. Thanks!
Here's the code on the files that's giving the error:
#include <iostream>
using namespace std;
int p,q;
int f( int, int,const int [][8],const int [][8], int [],int []);
This happens if you haven't provided an implementation of your f(..) function.
In your main.cpp file, simply implement the function, like:
int f( int, int,const int [][8],const int [][8], int [],int [])
{
// Do stuff...
}

Resources