Link errors compiling a mixed objective-C/C++ project with C++11 support in xcode 4.5.2

左心房为你撑大大i 提交于 2020-01-03 01:55:11

问题


I am trying to compile a workspace of 3 projects with C++11 support. This set of projects has successfully compiled and linked using LLVM compiler defaults. In addition, the c++ code has previously compiled on a number of compilers including g++, llvm, msvc, sun, irix etc so it is very clean code.

Under the c++ language dialect section of the workspace build settings there are 3 line items:

c language dialect
c++ language dialect
c++ standard library

Using the settings:

c language dialect: compiler default
c++ language dialect: compiler default
c++ standard library: libc++ (LLVM c++ standard library with c++11 support)

I can successfully compile but get many link errors. Some are from our own functions and some are from standard functions. Here is a sample of link errors to standard functions:

(null): "std::string::find_last_of(char const*, unsigned long) const", referenced from: (null): "std::string::size() const", referenced from:

It seemed clear that I was linking to the wrong standard library, so I investigated changing various options above. Changing the dialect to -std=c++11 even broke my compilation. I could not find a combination that worked for me.

Can you suggest what might be wrong?

EDIT: my motivation for doing this is that I want to use std::thread in my c++ code that interfaces with objective-c (and have that c++ code still be portable)


回答1:


XCode 4.5.2 / OSX 10.8.2

I've been getting these

(null): "std::string::find_last_of(char const*, unsigned long) const", 
 referenced from: (null):    "std::string::size() const", referenced from:

Variously with different versions of openCV libraries. It seems like the issue is access to the C++ standard library. In this case working settings were as follows:

Older library versions:

C Language Dialect GNU99[-std=gnu99]
C++ Language Dialect GNU++11[-std=gnu++11]
C++ Standard Library libstdc++ (GNU C++ standard library)

recent library versions:

C Language Dialect GNU99[-std=gnu99]
C++ Language Dialect GNU++11[-std=gnu++11]
C++ Standard Library libc++ (LLVM C++ standard library with C++11 support)

Toggling the standard library spec to either version yields those errors.




回答2:


I finally traced the problem to two things

1) Despite the fact I set the compiler options at the workspace level, these options were not being propagated to the constituent projects. Something to be aware of! So in effect different projects were being compiled with different sets of compiler libraries and dialects - so not surprising there were link errors. The combination of options that worked for me were:

C Language Dialect Compiler Default
C++ Language Dialect -std=c++11
C++ Standard Library libc++ (LLVM C++ standard library with C++11 support)

2) Under c++11 compilation, one of my projects would not compile. After fixing the syntax error, it compiled and linked fine with the above options




回答3:


While use Code 5 i had a similar problem

Undefined symbols for architecture armv7:
"std::string::clear()", referenced from:
FormatLog(unsigned int, int, char const*, std::string&) in libMO.a(AppLog.o)

Changing "Deployment Target" from 7.0 to 6.0 helped me.

For XCode 4.5 it seems have to use LLVM C++ with C++11 support compiler instead GNU C++ compiler for C++ Standard Library.



来源:https://stackoverflow.com/questions/14246622/link-errors-compiling-a-mixed-objective-c-c-project-with-c11-support-in-xcod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!