无法在makefile中包含库

时间:2016-11-23 16:40:44

标签: makefile linker

我在使用 make。

时有点新鲜

我有一个用C语言编写的库,它位于文件夹 folder1 中  这些文件的位置还有一个makefile,如下所示:

PREFIX ?= /usr/local

CC = gcc
AR = ar

CFLAGS = -std=gnu99 -fPIC -Wall -Wno-unused-parameter -Wno-unused-function -I. -O4

APRILTAG_SRCS := $(shell ls *.c common/*.c)
APRILTAG_HEADERS := $(shell ls *.h common/*.h /usr/include/flycapture/*.h)
APRILTAG_OBJS := $(APRILTAG_SRCS:%.c=%.o)
TARGETS := libapriltag.a libapriltag.so

.PHONY: all
all: $(TARGETS)
    @$(MAKE) -C example all

.PHONY: install
install: libapriltag.so
    @chmod +x install.sh
    @./install.sh $(PREFIX)/lib libapriltag.so
    @./install.sh $(PREFIX)/include/apriltag $(APRILTAG_HEADERS)
    @sed 's:^prefix=$$:prefix=$(PREFIX):' < apriltag.pc.in > apriltag.pc
    @./install.sh $(PREFIX)/lib/pkgconfig apriltag.pc
    @rm apriltag.pc
    @ldconfig

libapriltag.a: $(APRILTAG_OBJS)
    @echo "   [$@]"
    @$(AR) -cq $@ $(APRILTAG_OBJS)

libapriltag.so: $(APRILTAG_OBJS)
    @echo "   [$@]"
    @$(CC) -fPIC -shared -o $@ $^

%.o: %.c
    @echo "   $@"
    @$(CC) -o $@ -c $< $(CFLAGS)

.PHONY: clean
clean:
    @rm -rf *.o common/*.o $(TARGETS)
    @$(MAKE) -C example clean

然后,在文件夹示例中,我有一些例子,我可以通过在终端中提供以下命令来运行:

./opencv_demo 

./apriltag_demo

在此文件夹示例中,我有另一个makefile,如下所示:

CC = gcc
CXX = g++

CPPFLAGS = -I..
CFLAGS = -std=gnu99 -Wall -Wno-unused-parameter -Wno-unused-function -O4
CXXFLAGS = -Wall -O4
LDFLAGS = -Lusr/include/flycapture -lpthread -lm 
INC_DIR = /usr/include/flycapture

TARGETS := apriltag_demo opencv_demo

.PHONY: all
all: apriltag_demo opencv_demo

apriltag_demo: apriltag_demo.o ../libapriltag.a
    @echo "   [$@]"
    @$(CC) -o $@ $^ $(LDFLAGS)

opencv_demo: opencv_demo.o ../libapriltag.a /usr/include/flycapture/Error.h
    @echo "   [$@]"
    @$(CXX) -o $@ $^ $(LDFLAGS) `pkg-config --libs opencv`

%.o: %.c
    @echo "   $@"
    @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS)

%.o: %.cc
    @echo "   $@"
    @$(CXX) -o $@ -c $< $(CXXFLAGS) $(CPPFLAGS)

.PHONY: clean
clean:
    @rm -rf *.o $(TARGETS)

如果我在 folder1 make ,我会收到以下错误:

opencv_demo.o: In function `main':
opencv_demo.cc:(.text.startup+0x24e): undefined reference to `FlyCapture2::Error::Error()'
opencv_demo.cc:(.text.startup+0x2a7): undefined reference to `FlyCapture2::Error::~Error()'
opencv_demo.cc:(.text.startup+0x11a0): undefined reference to `FlyCapture2::Error::~Error()'
collect2: error: ld returned 1 exit status
make[1]: *** [opencv_demo] Error 1
make[1]: Leaving directory `/home/fschiano/Repositories/apriltag2/example'
make: *** [all] Error 2

如果我没有错,这个错误告诉我我没有包含库。

opencv_demo.cc 文件中,我做了:

#include "flycapture/FlyCapture2.h"
#include "flycapture/Error.h"
using namespace FlyCapture2;

/ usr / include / flycapture中的标题包含文件 opencv_demo.cc 中所需的所有标题的文件夹。

我不知道为什么不工作,有人能帮助我吗?

我想我应该在makefile中写一下搜索库但我不知道怎么做。

我试着分别在makefile中编写以下内容,但它们不起作用:

LDFLAGS = -Lusr/include/flycapture -lpthread -lm 
INC_DIR = /usr/include/flycapture
DEPS = $(INC_DIR)/Error.h

0 个答案:

没有答案