matlab mex clang C ++ 11 thread - >未定义符号错误 [英] matlab mex clang C++11 thread -> Undefined symbols error

查看:652
本文介绍了matlab mex clang C ++ 11 thread - >未定义符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我想使用C ++ 11的线程STL的Matlab mex文件(R2013a)使用Xcode 4.6



我修改〜/ .matlab / R2013a / mexopts.sh

  CC ='clang ++'#was llvm-gcc-4.2 
CXX ='clang ++'#was llvm-g ++ - 4.2
MACOSX_DEPLOYMENT_TARGET = '10 .8'#为10.5。支持C ++ 11> = 10.7
CXXFLAGS =$ CXXFLAGS -std = gnu ++ 11 -stdlib = libc ++#附加标志

没有C ++ 11功能的正常mex文件编译得很好。此外,除了链接失败,编译器很好地检测到STL。

 > mex mextest.cpp 

架构x86_64的未定义符号:
std :: __ 1 :: __ thread_struct :: __ thread_struct(),引用自:
void * std :: __ 1 :: __ thread_proxy< std :: __ 1 :: tuple< void(*)()> >(void *)in mextest.o
std :: __ 1 :: __ thread_struct ::〜__thread_struct(),引用自:
void * std :: __ 1 :: __ thread_proxy< std :: __1 :: tuple< void(*)()> >(void *)in mextest.o
std :: __ 1 :: __ thread_local_data(),引用自:
void * std :: __ 1 :: __ thread_proxy< std :: __ 1 :: tuple< ; void(*)()> >(void *)in mextest.o
std :: __ 1 :: __ throw_system_error(int,char const *),引用自:
_mexFunction in mextest.o
std: :__ 1 :: thread :: join(),引用自:
_mexFunction in mextest.o
std :: __ 1 :: thread ::〜thread(),引用自:
_mexFunction in mextest.o
ld:没有为架构x86_64找到符号
clang:error:linker命令失败,退出代码为1(使用-v查看调用)

mex:mextest.mexmaci64的链接失败。

使用mex时出错(第206行)
无法成功完成。

实际的源代码如下所示。细节不重要,因为它编译良好在Matlab R2013 WINDOWS版本与Visual Studio 2012 Express。一个等效的cpp也是用clang ++ -std = gnu ++ 11 -stdlib = libc ++ clangtest.cpp编译得到的。所以,至少,在代码中没有逻辑错误(我不是说它是安全的代码,它只是一个测试。)

  #includemex.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\\\
,count_thread1,count_thread2); //不确定printf在Matlab中是否是线程安全的。但它在这个特定的例子中工作
}
}
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 = + j-1;
}
count_thread1 ++;
mexPrintf(1:%d,%d\\\
,count_thread1,count_thread2);
}
mexPrintf(\\\
);
t.join();
mexPrintf(Done \\\
);
}

看起来我需要替换一些include目录和/应该修改哪些选项?



谢谢。

解决方案

错误是由于编译 -stdlib = libc ++ 但链接到 -lstdc ++ 。您可以通过以下两种方式之一进行修复:


  1. 修复它 mexopts.sh 。最激烈和有效的解决方案。位于〜/ .matlab / $ {MATLAB_VERSION} /mexopts.sh ,这将确定所有编译器选项。


  2. Patchwork解决方案:只需在尾部添加 -lc ++ CXXLIBS 结尾。我不知道链接到多个版本的标准库的效果,但它似乎工作。在您的mex调用中,添加参数 CXXLIBS =\ $ CXXLIBS -lc ++


作为第二个问题,我相信你完全覆盖了 CXXFLAGS 的价值;您必须转义 $ 符号,如上面对库的操作。


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:

  1. 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++.

  2. 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.

这篇关于matlab mex clang C ++ 11 thread - &gt;未定义符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆