使用 clang/LLVM 通过 LDFLAGS 链接 .dylib

linking .dylib via LDFLAGS using clang/LLVM

本文关键字:LDFLAGS 链接 dylib 通过 LLVM clang 使用      更新时间:2023-10-16

我收到一个错误。

其中一个源文件引用:

#include <libxml/parser.h>

我正在使用下面的 Makefile,尝试链接:

LDFLAGS =-l../../usr/local/sys/usr/lib/libxml2.dylib

路径似乎正确,并且文件在那里。

来自 IDE 的错误详细信息:http://clip2net.com/clip/m0/1333837472-clip-29kb.pnghttp://clip2net.com/clip/m0/1333837744-clip-32kb.png

我做错了什么?

#############################################################################
# Makefile for iPhone application (X)
#############################################################################
# Define here the name of your project's main executable, and the name of the
# build directory where the generated binaries and resources will go.
NAME    = X
OUTDIR  = X.app
# Define here the minimal iOS version's MAJOR number (iOS3, iOS4 or iOS5)
IOSMINVER = 5
# List here your project's resource files. They can be files or directories.
RES = Info.plist icon.png
# Define here the compile options and the linker options for your project.
CFLAGS  = -W -Wall -O2 -Icocos2dx/include -Icocos2dx/platform -Icocos2dx/platform/ios -Icocos2dx/effects -Icocos2dx/cocoa -Icocos2dx/support -Icocos2dx/support/image_support -Icocos2dx/support/data_support -Icocos2dx/support/zip_support -Icocos2dx/extensions -Icocos2dx
LDFLAGS =-l../../usr/local/sys/usr/lib/libxml2.2.dylib
#############################################################################
# Except for specific projects, you shouldn't need to change anything below
#############################################################################
# Define which compiler to use and what are the mandatory compiler and linker
# options to build stuff for iOS. Here, use the ARM cross-compiler for iOS,
# define IPHONE globally and link against all available frameworks.
CC  = clang
LD  = link
CFLAGS  += -ccc-host-triple arm-apple-darwin -march=armv6 --sysroot ../../usr/local/sys -integrated-as -fdiagnostics-format=msvc -fconstant-cfstrings -DIPHONE -D__IPHONE_OS_VERSION_MIN_REQUIRED=$(IOSMINVER)0000
LDFLAGS += -lstdc++ $(addprefix -framework , $(notdir $(basename $(wildcard /Frameworks/iOS$(IOSMINVER)/*))))
# List here your source files. The current rule means: ask the shell to build
# a one-liner list of all files in the current directory and its subdirectories
# ending with either .c, .cc, .cpp, .cxx, .m, .mm, .mx or .mxx.
SRC = $(shell find . ( -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.m" -o -name "*.mm" -o -name "*.mx" -o -name "*.mxx" ) -printf '%p ')
# Define where the object files should go - currently, just aside the source
# files themselves. We take the source file's basename and just append .o.
OBJ = $(addsuffix .o, $(basename $(SRC)))
###################
# Rules definitions
# This rule is the default rule that is called when you type "make". It runs
# the specified other rules in that order: removing generated output from
# previous builds, compiling all source files into object files, linking them
# all together, codesigning the generated file, copying resources in place
# and then displaying a success message.
all:    prune $(OBJ) link codesign resources checksum ipa deb end
# The following rule removes the generated output from any previous builds
prune:
    @echo " + Pruning compilation results..."
    @rm -f $(OUTDIR)/$(NAME)
# The following rules compile any .c/.cc/.cpp/.cxx/.m/.mm/.mx/.mxx file it
# finds in object files (.o). This is to handle source files in different
# languages: C/C++ (with .c* extension), and Objective-C (.m*).
%.o:    %.c
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.cc
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.cpp
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.cxx
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.m
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.mm
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.mx
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
%.o:    %.mxx
    @echo " + Compiling $<..."; $(CC) $(CFLAGS) -o $@ -c $<
# The following rule first ensures the output directory exists, creates it if
# necessary, then links the compiled .o files together in that directory
link:
    @echo " + Linking project files..."
    @test -d $(OUTDIR) || mkdir -p $(OUTDIR)
    @$(LD) $(LDFLAGS) -o $(OUTDIR)/$(NAME) $(OBJ)
# The following rule calls Saurik's ldid code pseudo-signing tool to generate
# the SHA checksums needed for the generated binary to run on jailbroken iOS.
codesign:
    @echo " + Pseudo-signing code..."
    @ldid -S $(OUTDIR)/$(NAME)
    @rm -f $(OUTDIR)/.$(NAME).cs
# The following rule takes all the specified resource items one after the
# other (whether they are files or directories) ; files are copied in place
# and directories are recursively copied only if they don't exist already.
resources:
    @echo " + Copying resources..."
    @for item in $(RES); do 
        if [ -d $$item ]; then test -d $(OUTDIR)/$$item || cp -r $$item $(OUTDIR)/; chmod +r $(OUTDIR)/$$item; 
        else cp $$item $(OUTDIR)/; chmod +r $(OUTDIR)/$$item; 
        fi; 
    done
    @chmod +x $(OUTDIR)
    @chmod -R +r $(OUTDIR)
    @chmod +x $(OUTDIR)/$(NAME)
# The following rule takes all files in the target directory and builds the
# _CodeSignature/CodeResource XML file with their SHA1 hashes.
checksum:
    @echo " + Generating _CodeSignature directory..."
    @echo -n APPL???? > $(OUTDIR)/PkgInfo
    @codesigner $(OUTDIR) > .CodeResources.$(NAME)
    @test -d $(OUTDIR)/_CodeSignature || mkdir -p $(OUTDIR)/_CodeSignature
    @mv .CodeResources.$(NAME) $(OUTDIR)/_CodeSignature/CodeResources
    @test -L $(OUTDIR)/CodeResources || ln -s _CodeSignature/CodeResources $(OUTDIR)/CodeResources
# The following rule builds an IPA file out of the compiled app directory.
ipa:
    @echo " + Building iTunes package..."
    @test -d Packages || mkdir Packages
    @rm -f Packages/$(NAME).ipa
    @test -d Payload || mkdir Payload
    @mv -f $(OUTDIR) Payload
    @cp -f iTunesArtwork.jpg iTunesArtwork
    @chmod +r iTunesArtwork
    @zip -y -r Packages/$(NAME).ipa Payload iTunesArtwork -x *.log *.lastbuildstate *successfulbuild > /dev/null
    @rm -f iTunesArtwork
    @mv -f Payload/$(OUTDIR) .
    @rmdir Payload
# The following rule builds a Cydia package out of the compiled app directory.
deb:
    @echo " + Building Cydia package..."
    @test -d Packages || mkdir Packages
    @rm -f Packages/$(NAME).deb
    @test -d $(NAME) || mkdir $(NAME)
    @test -d $(NAME)/Applications || mkdir $(NAME)/Applications
    @mv -f $(OUTDIR) $(NAME)/Applications
    @test -d $(NAME)/DEBIAN || mkdir $(NAME)/DEBIAN
    @cp -f cydia-package.cfg $(NAME)/DEBIAN/control
    @chmod +r $(NAME)/DEBIAN/control
    @echo "#!/bin/bash" > $(NAME)/DEBIAN/postinst
    @echo "rm -f /Applications/$(OUTDIR)/*.log /Applications/$(OUTDIR)/*.lastbuildstate /Applications/$(OUTDIR)/*.successfulbuild" >> $(NAME)/DEBIAN/postinst
    @echo "chown -R root:admin "/Applications/$(OUTDIR)"" >> $(NAME)/DEBIAN/postinst
    @echo "find "/Applications/$(OUTDIR)"|while read ITEM; do if [ -d "$$ITEM" ]; then chmod 755 "$$ITEM"; else chmod 644 "$$ITEM"; fi; done" >> $(NAME)/DEBIAN/postinst
    @echo "chmod +x "/Applications/$(OUTDIR)/$(NAME)"" >> $(NAME)/DEBIAN/postinst
    @echo "su -c /usr/bin/uicache mobile 2> /dev/null" >> $(NAME)/DEBIAN/postinst
    @echo "exit 0" >> $(NAME)/DEBIAN/postinst
    @chmod +r+x $(NAME)/DEBIAN/postinst
    @dpkg-deb -b $(NAME) > /dev/null 2>&1
    @mv -f $(NAME).deb Packages
    @mv -f $(NAME)/Applications/$(OUTDIR) .
    @rm -rf $(NAME)
# This simple rule displays the success message after a successful build
end:
    @echo " + Done. Output directory is "$(OUTDIR)", iTunes package is "Packages$(NAME).ipa", Cydia package is "Packages$(NAME).deb"."
# This rule removes generated object files from the project and also temporary
# files ending with ~ or #.
clean:
    @echo " + Cleaning project intermediate files..."
    @rm -f $(OBJ) *~ *#
    @echo " + Done."
# This rule removes all generated output from any previous builds so as to
# leave an intact source tree (useful for generating source tree releases).
distclean: clean
    @echo " + Cleaning project output files..."
    @rm -rf $(OUTDIR)
    @echo " + Done."

实际上,您可能有两个问题。我建议你试试:

-I../../usr/local/sys/usr/include添加到 CFLAGS 以使其找到标头。

LDFLAGS更改为 -L../../usr/local/sys/usr/lib ,添加LIBS=-lxml2并将链接器调用更改为$(LD) $(LDFLAGS) -o $(OUTDIR)/$(NAME) $(OBJ) $(LIBS)(即在链接器命令行的末尾添加-l,在链接器命令行的开头添加-L)。

-l 仅用于指定库名称。使用 -L 添加还将在其中查找库的目录:

LDFLAGS += -L../../usr/local/sys/usr/lib -lxml2

希望这有帮助。