如何链接在Xcode10中本地构建的动态库?

时间:2018-11-07 13:46:16

标签: xcode macos dynamic-linking dynamic-library

我在本地动态构建了openCV和其他库。这些库包含在C ++项目中。我正在使用Makefile来编译项目。在Makefile使用库中,我使用了一些标志:-L指向动态库所在的文件夹,然后为要包含的每个动态库-l-I进行包含标头。

对于某些库,pkg-config可用,因此更加简单。

在构建运行程序之后,我还需要使用export终端命令导出库:

export DYLD_LIBRARY_PATH=../cmake_bin_dir/instDir/lib/:../poco/instDir/lib/
echo $DYLD_LIBRARY_PATH

我想为此项目使用Xcode。我设法执行的唯一步骤是包含标题。

如何在Xcode中动态链接本地构建的库?必须执行与Makefile中的步骤等效的哪些步骤?

例如,这是我得到的错误:

Ld /Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Products/Debug/MyProject2 normal x86_64 (in target: MyProject2)
    cd /Users/user/Documents/XCodeMyProject/MyProject2
    export MACOSX_DEPLOYMENT_TARGET=10.13
    /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.14.sdk -L/Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Products/Debug -L/Users/user/Downloads/openCVProject/SDL2-2.0.8/build/lib -L/Users/user/Downloads/openCVProject/cmake_bin_dir/instDir/lib -L/Users/user/Downloads/openCVProject/SDL2-2.0.8/build/lib -L/Users/user/Downloads/openCVProject/poco/instDir/lib -L/Users/user/Downloads/openCVProject/jsoncpp/build/lib -F/Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Products/Debug -filelist /Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Intermediates.noindex/MyProject2.build/Debug/MyProject2.build/Objects-normal/x86_64/MyProject2.LinkFileList -mmacosx-version-min=10.13 -Xlinker -object_path_lto -Xlinker /Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Intermediates.noindex/MyProject2.build/Debug/MyProject2.build/Objects-normal/x86_64/MyProject2_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -lSDL2 -lopencv_calib3d.4.0.0 -lopencv_calib3d.4.0.0 -lopencv_calib3d.4.0.0 -lopencv_core.4.0.0 -lopencv_core.4.0.0 -lopencv_core.4.0.0 -lopencv_dnn.4.0.0 -lopencv_dnn.4.0.0 -lopencv_dnn.4.0.0 -lopencv_features2d.4.0.0 -lopencv_features2d.4.0.0 -lopencv_features2d.4.0.0 -lopencv_flann.4.0.0 -lopencv_flann.4.0.0 -lopencv_flann.4.0.0 -lopencv_highgui.4.0.0 -lopencv_highgui.4.0.0 -lopencv_highgui.4.0.0 -lopencv_imgcodecs.4.0.0 -lopencv_imgcodecs.4.0.0 -lopencv_imgcodecs.4.0.0 -lopencv_imgproc.4.0.0 -lopencv_imgproc.4.0.0 -lopencv_imgproc.4.0.0 -lopencv_ml.4.0.0 -lopencv_ml.4.0.0 -lopencv_ml.4.0.0 -lopencv_objdetect.4.0.0 -lopencv_objdetect.4.0.0 -lopencv_objdetect.4.0.0 -lopencv_photo.4.0.0 -lopencv_photo.4.0.0 -lopencv_photo.4.0.0 -lopencv_shape.4.0.0 -lopencv_shape.4.0.0 -lopencv_shape.4.0.0 -lopencv_stitching.4.0.0 -lopencv_stitching.4.0.0 -lopencv_stitching.4.0.0 -lopencv_superres.4.0.0 -lopencv_superres.4.0.0 -lopencv_superres.4.0.0 -lopencv_video.4.0.0 -lopencv_video.4.0.0 -lopencv_video.4.0.0 -lopencv_videoio.4.0.0 -lopencv_videoio.4.0.0 -lopencv_videoio.4.0.0 -lopencv_videostab.4.0.0 -lopencv_videostab.4.0.0 -lopencv_videostab.4.0.0 -Xlinker -dependency_info -Xlinker /Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Intermediates.noindex/MyProject2.build/Debug/MyProject2.build/Objects-normal/x86_64/MyProject2_dependency_info.dat -o /Users/user/Library/Developer/Xcode/DerivedData/MyProject2-cfkbveclaeyyswbyyzjdzbcpreoz/Build/Products/Debug/MyProject2

上面显示的是命令-L/Users/user/Downloads/openCVProject/SDL2-2.0.8/build/lib,其中指示的路径包含文件libSDL2.dylib libSDL2d.dylib pkgconfig。唯一的问题可能是-lSDL2重复了两次。a

这是我正在使用的makefile:

CXX = g++

CXXFLAGS = -std=c++11
INC_PATH = `pkg-config --cflags ../cmake_bin_dir/instDir/lib/pkgconfig/opencv.pc` \
    `pkg-config --cflags ../SDL2-2.0.8/build/lib/pkgconfig/sdl2.pc` \
    `pkg-config --cflags ../jsoncpp/build/pkg-config/jsoncpp.pc` \
    -I ../poco/instDir/include/


#LIB_PATH = -L../cmake_bin_dir/lib/ ./gainput/build/lib -L../SDL2-2.0.8/build/ -L../SDL2-2.0.8/build/lib
LIBS =  `pkg-config --libs ../cmake_bin_dir/instDir/lib/pkgconfig/opencv.pc` \
    `pkg-config --libs ../SDL2-2.0.8/build/lib/pkgconfig/sdl2.pc` \
    `pkg-config --libs ../jsoncpp/build/pkg-config/jsoncpp.pc` \
    -L../poco/instDir/lib/ -lPocoNetd -lPocoUtild -lPocoFoundationd

SOURCEDIR := ./
SOURCES := $(wildcard $(SOURCEDIR)/*.cpp)
OBJDIR=$(SOURCEDIR)/obj

OBJECTS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.d,$(SOURCES))

# ADD MORE WARNINGS!
WARNING := -Wall -Wextra

# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean

# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: parking

clean:
    $(RM) $(OBJECTS) $(DEPENDS) parking

# Linking the executable from the object files
parking: $(OBJECTS)
    $(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)

-include $(DEPENDS)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile | $(OBJDIR)
    $(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $@

0 个答案:

没有答案