使用外部库构建 Xcode 项目时出现链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12911408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:49:58  来源:igfitidea点击:

Linker Error when building Xcode project using external library

c++xcodelinker

提问by Ned W.

I am attempting to build a very simple command line application, in Xcode, that will print out basic information about MXF video files. In order to do this I need to use the libmxf, libbmx, and libbmx libraries available for download here:

我正在尝试在 Xcode 中构建一个非常简单的命令行应用程序,它将打印出有关 MXF 视频文件的基本信息。为此,我需要使用可在此处下载的 libmxf、libbmx 和 libbmx 库:

http://sourceforge.net/p/bmxlib/home/Home/

http://sourceforge.net/p/bmxlib/home/Home/

My C++ code is incredibly simple at this point:

在这一点上,我的 C++ 代码非常简单:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <vector>

#include <bmx/mxf_reader/MXFFileReader.h>
#include <bmx/mxf_reader/MXFGroupReader.h>
#include <bmx/mxf_reader/MXFSequenceReader.h>
#include <bmx/mxf_reader/MXFFrameMetadata.h>
#include <bmx/MXFUtils.h>
#include <bmx/Utils.h>

using namespace std;
using namespace bmx;

#define MXF_OPEN_READ(fn, pf)   mxf_disk_file_open_read(fn, pf)

int main(int argc, const char * argv[])
{
    std::vector<const char *> filenames;
    std::cout << "mxfheader: execution beginning...\n";
    for (int cmdln_index = 0; cmdln_index < argc; cmdln_index++) {
        if (!check_file_exists(argv[cmdln_index])) {
            if (argv[cmdln_index][0] == '-') {
                fprintf(stderr, "Unknown argument '%s'\n", argv[cmdln_index]);
            } else {
                fprintf(stderr, "Failed to open input filename '%s'\n", argv[cmdln_index]);
            }
            return 1;
        }
        filenames.push_back(argv[cmdln_index]);
    }

    std::cout << filenames[0] << "\n";
    return 0;
}

When I compiled the BMX library, I was sure to run configure with 64-bit support, like so:

当我编译 BMX 库时,我肯定会在 64 位支持下运行 configure,如下所示:

./configure --build=x86_64-apple-darwin11.4.2 --host=x86_64-apple-darwin11.4.2 CFLAGS="-arch x86_64" CXXFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" CC=clang CXX=clang++

In the XCode Project under Build Settings, I have added /usr/local/lib to my Search Paths. Under Build Phases, I have added "libbmx-0.1.3.dylib", "libMXF-1.0.4.dylib", and "libMXF++-1.0.4.dylib" to the "Link Binary With Libraries" section.

在构建设置下的 XCode 项目中,我已将 /usr/local/lib 添加到我的搜索路径。在 Build Phases 下,我已将“libbmx-0.1.3.dylib”、“libMXF-1.0.4.dylib”和“libMXF++-1.0.4.dylib”添加到“Link Binary With Libraries”部分。

I have verified that these libraries are, indeed, 64-bit ( file libbmx-0.1.3.dylib returns libbmx-0.1.3.dylib: Mach-O 64-bit dynamically linked shared library x86_64 ).

我已经验证这些库确实是 64 位的(文件 libbmx-0.1.3.dylib 返回 libbmx-0.1.3.dylib: Mach-O 64 位动态链接共享库 x86_64 )。

Every time I try and build the application, I get the following linker error:

每次我尝试构建应用程序时,都会收到以下链接器错误:

Ld /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader normal x86_64
    cd /Users/ned/Documents/src/mxfheader
    setenv MACOSX_DEPLOYMENT_TARGET 10.7
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -L/usr/local/lib -F/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -filelist /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Intermediates/mxfheader.build/Debug/mxfheader.build/Objects-normal/x86_64/mxfheader.LinkFileList -mmacosx-version-min=10.7 -stdlib=libc++ -lbmx-0.1.3 -lMXF-1.0.4 -lMXF++-1.0.4 -o /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader

Undefined symbols for architecture x86_64:
  "bmx::check_file_exists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", 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)

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

回答by Petesh

Your problem is the option: -stdlib=libc++in the command line. It's causing a link to the wrong libc++, you need to make it -stdlib=libstdc++, as this is the stdlib that the libbmxlibrary is compiled against.

您的问题是选项:-stdlib=libc++在命令行中。它导致链接到错误的 libc++,您需要创建它-stdlib=libstdc++,因为这libbmx是编译库所针对的 stdlib 。

under the Apple LLVM compileroptions for the C++ standard library, select: libstdc++, or pick compiler default(which should choose libstdc++ also)

Apple LLVM compilerC++ 标准库的选项下,选择:libstdc++或选择compiler default(也应该选择 libstdc++)