Link 动态库

Link dylib library

我正在尝试 link 一个 dylib 到我在 Mac 上的 makefile,但是 Clang 给出了这个消息:

Undefined symbols for architecture x86_64:
"_zbesj_wrap", referenced from:
  sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
"_zbesy_wrap", referenced from:
  sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我在 /usr/lib 中安装了库,在那里我可以看到 libcomplex_bessel.0.6.0.dylib 和 libcomplex_bessel.dylib。 这是我的生成文件:

OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test

all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o $@
main.o: main.cpp
    $(CC) $(CFLAGS) -c $< -o $@ 
besselJ.o: besselJ.cpp
    $(CC) $(CFLAGS) -c $< -o $@ $(LIBS)

阅读其他问题后,我尝试了 -L 和 -l 的不同组合,但没有任何效果。抱歉,这是我第一次使用外部库...


我将 makefile 更改为:

OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test

all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o $@ $(LIBS)
main.o: main.cpp
    $(CC) $(CFLAGS) -c $< -o $@ 
besselJ.o: besselJ.cpp
    $(CC) $(CFLAGS) -c $< -o $@ 

但我仍然遇到问题,我收到此消息:

c++ -std=c++11 -stdlib=libc++ main.o besselJ.o -o test -L/usr/lib -lcomplex_bessel
ld: library not found for -lcomplex_bessel
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

好的,我想我使用 xcode-select --install 解决了它:其他用户在更新库后遇到了同样的问题。

您需要将库传递给链接器,而不是编译器:

OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test

all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
    $(CC) $(OBJS) -o $@ $(LIBS)
main.o: main.cpp
    $(CC) $(CFLAGS) -c $< -o $@ 
besselJ.o: besselJ.cpp
    $(CC) $(CFLAGS) -c $< -o $@