在 Makefile 的链接过程中添加库的问题
Posted
技术标签:
【中文标题】在 Makefile 的链接过程中添加库的问题【英文标题】:problem in adding libraries during linking process in Makefile 【发布时间】:2019-07-27 08:32:01 【问题描述】:我是 Makefile 的新手。我在一个项目中使用 opencv 和 lpthread 库。以下 makefile 代码似乎不起作用。似乎链接过程失败了。请指出我在这里犯的错误。
操作系统:Ubuntu 18.04
CC=g++
LIBS=`pkg-config --cflags --libs opencv`
CXXFLAGS= -c -Wall -std=c++17 -lpthread -march=native -mtune=native -O2
CXXFLAGS2= -Wall -std=c++17 -lpthread -march=native -mtune=native -O2
all: a.out
a.out: main.o brain_starter.o brain_class.o segment_class.o \
logic_core.o data_package_class.o core_class.o
$(CC) $(CXXFLAGS2) $(LIBS) -o a.out main.o brain_starter.o brain_class.o \
segment_class.o logic_core.o data_package_class.o core_class.o
main.o: main.cpp dependency_functions.h
$(CC) $(CXXFLAGS) main.cpp
brain_starter.o: brain_starter.cpp
$(CC) $(CXXFLAGS) brain_starter.cpp
brain_class.o: brain_class.cpp
$(CC) $(CXXFLAGS) brain_class.cpp
segment_class.o: segment_class.cpp
$(CC) $(CXXFLAGS) segment_class.cpp
logic_class.o: logic_class.cpp
$(CC) $(CXXFLAGS) logic_class.cpp
data_package_class.o: data_package_class.cpp
$(CC) $(CXXFLAGS) $(LIBS) data_package_class.cpp
core_class.o: core_class.cpp
$(CC) $(CXXFLAGS) core_class.cpp
clean:
rm -rf *o a.out
【问题讨论】:
【参考方案1】:我重新排列了链接代码中的库并且生成文件正常工作。这是工作代码。
CC=g++
OPENCVLIBS=`pkg-config --cflags --libs opencv`
LIBS= -lpthread
CXXFLAGS= -c -Wall -std=c++17 -march=native -mtune=native -O2
CXXFLAGS2= -Wall -std=c++17 -lpthread -march=native -mtune=native -O2
all: a.out
a.out: main.o brain_starter.o brain_class.o segment_class.o \
logic_core.o data_package_class.o core_class.o
$(CC) $(CXXFLAGS2) -o a.out main.o brain_starter.o brain_class.o segment_class.o logic_core.o data_package_class.o core_class.o $(LIBS) $(OPENCVLIBS)
main.o: main.cpp dependency_functions.h
$(CC) $(CXXFLAGS) $(LIBS) main.cpp
brain_starter.o: brain_starter.cpp
$(CC) $(CXXFLAGS) $(LIBS) brain_starter.cpp
brain_class.o: brain_class.cpp
$(CC) $(CXXFLAGS) $(LIBS) brain_class.cpp
segment_class.o: segment_class.cpp
$(CC) $(CXXFLAGS) $(LIBS) segment_class.cpp
logic_class.o: logic_class.cpp
$(CC) $(CXXFLAGS) $(LIBS) logic_class.cpp
data_package_class.o: data_package_class.cpp
$(CC) $(CXXFLAGS) $(LIBS) $(OPENCVLIBS) data_package_class.cpp
core_class.o: core_class.cpp
$(CC) $(CXXFLAGS) core_class.cpp
clean:
rm -rf *o a.out
【讨论】:
是的,目标文件和库在链接命令行上的列出顺序很重要。通常,一个目标文件或库中尚未解析的符号只能由稍后在命令行中列出的对象解析。以上是关于在 Makefile 的链接过程中添加库的问题的主要内容,如果未能解决你的问题,请参考以下文章