将Objective-C应用程序链接到c++静态库。

时间:2022-09-07 07:58:41

I am trying to use Xcode to build an Objective-C desktop application that links against a static C++ library. I am using the Apple's clang compiler. I am getting the following linker error:

我正在尝试使用Xcode来构建一个Objective-C桌面应用程序,它链接到一个静态c++库。我正在使用苹果的clang编译器。我得到了以下链接错误:

Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.2.0
Thread model: posix
 "/Developer/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.7.0 -syslibroot /Developer/SDKs/MacOSX10.7.sdk -o /Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug/sl marketplace analyitics.app/Contents/MacOS/sl marketplace analyitics -lcrt1.10.6.o -L/Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug -L/Users/andrew/Projects/sl-marketplace-analysis/platform/mac/sl marketplace analyitics/../../../../../Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug -filelist /Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Intermediates/sl marketplace analyitics.build/Debug/sl marketplace analyitics.build/Objects-normal/x86_64/sl marketplace analyitics.LinkFileList -framework Cocoa -lcore -lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a -F/Users/andrew/Library/Developer/Xcode/DerivedData/sl-marketplace-analysis-exomzzahbygseghhwoeclcvpooeo/Build/Products/Debug
Undefined symbols for architecture x86_64:
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in libcore.a(test.o)
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in libcore.a(test.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems like it isn't linking against the C++ std library. Is this the problem and if so, how to I ensure that it links against it?

看起来它并没有链接到c++ std库。这是问题吗?如果是,我如何确保它与它有关联?

Update:

更新:

If I add a blank cpp file to the build then everything compiles and runs fine. I just seem to need a way to tell the linker to link against the C++ std library. I have tried looking through all of the build settings in Xcode but can't seem to find anything that helps.

如果我在构建中添加一个空白的cpp文件,那么所有的内容都可以编译并运行良好。我似乎需要一种方法来告诉链接器链接到c++ std库。我尝试过在Xcode中查看所有的构建设置,但是似乎找不到任何有用的东西。

4 个解决方案

#1


17  

Try adding -lstdc++ or -lc++ (depending on which C++ runtime your static library expects) to "Other Linker Flags" under "Build Setting":

尝试添加-lstdc++或-lc++(取决于您的静态库期望的c++运行时)到“构建设置”下的“其他链接标记”:

将Objective-C应用程序链接到c++静态库。

#2


2  

Having same issue with Xcode 6, i have solved it by linking "libstdc++.6.0.9.dylib" in the "Link Binary with Libraries" build phase & adding -lstdc++ to "Other Linker Flags" under Build Setting

与Xcode 6有相同的问题,我通过链接“libstdc+ .6.0.9”解决了这个问题。dylib“在与库的链接二进制文件中”构建阶段&在构建设置下添加-lstdc++到“其他链接器标志”。

#3


1  

It seems weird that libcore.a is looking for a class named Init in the std::ios_base namespace. I don't think std::ios_base::Init is a standard class.

libcore似乎很奇怪。a在std中寻找名为Init的类::ios_base名称空间。我不认为std::ios_base::Init是一个标准类。

Aside from that, you might want to check that libcore.a actually includes x86_64 code. You can use the file command to check. For example:

除此之外,您可能还需要检查libcore。实际上包含了x86_64代码。您可以使用文件命令来检查。例如:

$ file /usr/lib/libz.dylib
/usr/lib/libz.dylib: Mach-O universal binary with 2 architectures
/usr/lib/libz.dylib (for architecture x86_64):  Mach-O 64-bit dynamically linked shared library x86_64
/usr/lib/libz.dylib (for architecture i386):    Mach-O dynamically linked shared library i386

If the file command output doesn't include a “for architecture x86_64” line, you can't use the library to create a 64-bit executable.

如果文件命令输出不包括“对于架构x86_64”行,那么您就不能使用库来创建64位的可执行文件。

#4


-1  

Other Linker Flags -lstdc++ Didn't help for me in Xcode5. But find out similar thing in Apple LLVM 5.0 - Lnaguage - C++

其他链接器标记-lstdc++在Xcode5中对我没有帮助。但在苹果LLVM 5.0 - Lnaguage - c++中也可以找到类似的东西。

default setting was libc++(LLVM C++ standard~blabla) and changed it to libstdc++(GNU thing)

默认设置是libc++(LLVM c++标准~blabla),并将其改为libstdc++(GNU thing)

Now there is no error with linking!!! and now I have to find out it runs fine.

现在链接没有错误!!!现在我必须发现它运行良好。

anyway thanks for advice.

总之谢谢你的建议。

#1


17  

Try adding -lstdc++ or -lc++ (depending on which C++ runtime your static library expects) to "Other Linker Flags" under "Build Setting":

尝试添加-lstdc++或-lc++(取决于您的静态库期望的c++运行时)到“构建设置”下的“其他链接标记”:

将Objective-C应用程序链接到c++静态库。

#2


2  

Having same issue with Xcode 6, i have solved it by linking "libstdc++.6.0.9.dylib" in the "Link Binary with Libraries" build phase & adding -lstdc++ to "Other Linker Flags" under Build Setting

与Xcode 6有相同的问题,我通过链接“libstdc+ .6.0.9”解决了这个问题。dylib“在与库的链接二进制文件中”构建阶段&在构建设置下添加-lstdc++到“其他链接器标志”。

#3


1  

It seems weird that libcore.a is looking for a class named Init in the std::ios_base namespace. I don't think std::ios_base::Init is a standard class.

libcore似乎很奇怪。a在std中寻找名为Init的类::ios_base名称空间。我不认为std::ios_base::Init是一个标准类。

Aside from that, you might want to check that libcore.a actually includes x86_64 code. You can use the file command to check. For example:

除此之外,您可能还需要检查libcore。实际上包含了x86_64代码。您可以使用文件命令来检查。例如:

$ file /usr/lib/libz.dylib
/usr/lib/libz.dylib: Mach-O universal binary with 2 architectures
/usr/lib/libz.dylib (for architecture x86_64):  Mach-O 64-bit dynamically linked shared library x86_64
/usr/lib/libz.dylib (for architecture i386):    Mach-O dynamically linked shared library i386

If the file command output doesn't include a “for architecture x86_64” line, you can't use the library to create a 64-bit executable.

如果文件命令输出不包括“对于架构x86_64”行,那么您就不能使用库来创建64位的可执行文件。

#4


-1  

Other Linker Flags -lstdc++ Didn't help for me in Xcode5. But find out similar thing in Apple LLVM 5.0 - Lnaguage - C++

其他链接器标记-lstdc++在Xcode5中对我没有帮助。但在苹果LLVM 5.0 - Lnaguage - c++中也可以找到类似的东西。

default setting was libc++(LLVM C++ standard~blabla) and changed it to libstdc++(GNU thing)

默认设置是libc++(LLVM c++标准~blabla),并将其改为libstdc++(GNU thing)

Now there is no error with linking!!! and now I have to find out it runs fine.

现在链接没有错误!!!现在我必须发现它运行良好。

anyway thanks for advice.

总之谢谢你的建议。