[英]GCC linking a static library
I have seen questions like these on SO but everyone has different answers and directory structures that aren't working for me. 我在SO上看到过类似的问题,但是每个人都有不同的答案和对我不起作用的目录结构。
My makefile: 我的makefile:
CC = g++
DEBUG = -g -std=c++11
TARGET = main
OBJECT_FILES = BingResultSet.o main.o
INC_PATH = -I HTTPClientLib/include
LIB_PATH = -L HTTPClientLib/lib/
start: clean BingResultSet.o main.o
$(CC) $(DEBUG) $(INC_PATH) $(LIB_PATH) $(OBJECT_FILES) -o $(TARGET)
rm -f *.o
BingResultSet.o: BingResultSet.cpp BingResultSet.h
$(CC) $(DEBUG) $(INC_PATH) $(LIB_PATH) -c BingResultSet.cpp
main.o: main.cpp
$(CC) $(DEBUG) $(INC_PATH) $(LIB_PATH) -c main.cpp
clean:
rm -f $(OBJECT_FILES) $(TARGET)
My file structure: 我的文件结构:
/Desktop/DataMiner/.cpp, .h, and makefile
/Desktop/DataMiner/HTTPClientLib/include/HTTPClient.h
/Desktop/DataMiner/HTTPClientLib/lib/HTTPClient.a
What's the correct way to link my static lib in my makefile? 在我的makefile中链接我的静态库的正确方法是什么?
Here's my $0.02: 这是我的$ 0.02:
you mix dependencies and build rules, instead, avoid repeating build rules: 您混合使用依赖项和构建规则,而要避免重复构建规则:
$(TARGET): $(OBJECT_FILES) $(CXX) $(DEBUG) $(INC_PATH) $^ -o $@ $(LIB_PATH) %.o: %.cpp $(CXX) $(DEBUG) $(INC_PATH) -c $< -o $@
You used CC
for a C++ compiler. 您将
CC
用于C ++编译器。 That's strange. 那很奇怪。 Use
CXX
使用
CXX
LDFLAGS
when you were just compiling LDFLAGS
You hardcoded the source and destination paths. 您对源路径和目标路径进行了硬编码。 Instead use the automatic variables (
$^
, $<
for source; $@
for destination) 而是使用自动变量(
$^
, $<
表示源; $@
表示目的地)
You tried to hardcode header dependencies. 您试图对标头依赖项进行硬编码。 That's error-prone and messes up source specification (you don't want
$^
to list .h
files in your command line...). 这容易出错,并且弄乱了源规范(您不希望
$^
在命令行中列出.h
文件...)。 Instead, use gcc -MM
¹ to generate the dependencies for you! 而是使用
gcc -MM
为您生成依赖项!
Next, do a conditional include of those dependencies: 接下来,对这些依赖项进行条件包含:
.depends: $(CXX) -MM $(CXXFLAGS) -c *.cpp > $@ -include .depends
It's usually handy to keep the .o files so you can speed up builds. 保留.o文件通常很方便,因此您可以加快构建速度。 Of course, this was not a good plan until you generated the header dependencies automatically.
当然,在您自动生成标头依赖项之前,这不是一个好计划。 If you insist, you can comment the
.PRECIOUS
target. 如果您坚持,则可以注释
.PRECIOUS
目标。 Intermediate targets are automatically deleted by GNU Make GNU Make自动删除中间目标
Here's the integrated offering I ended up with: 这是我最终得到的集成产品:
CXX = g++
TARGET = main
OBJECT_FILES = BingResultSet.o main.o
INC_PATH = -I HTTPClientLib/include
LIB_PATH = -L HTTPClientLib/lib/
CPPFLAGS = -g -std=c++11
CPPFLAGS+= $(INC_PATH)
# standard derived flags:
CXXFLAGS+=$(CPPFLAGS)
LDFLAGS+=$(LIB_PATH)
start: .depends $(TARGET)
$(TARGET): $(OBJECT_FILES)
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f .depends $(OBJECT_FILES) $(TARGET)
# to keep the .o files:
.PRECIOUS: $(OBJECT_FILES)
.depends:
$(CXX) -MM $(CXXFLAGS) -c *.cpp > $@
-include .depends
On a very simple sample set of files you get: 在一组非常简单的文件示例中,您将获得:
$ make clean
rm -f .depends BingResultSet.o main.o main
$ make
g++ -MM -g -std=c++11 -I HTTPClientLib/include -c *.cpp > .depends
g++ -I HTTPClientLib/include -c BingResultSet.cpp -o BingResultSet.o
g++ -I HTTPClientLib/include -c main.cpp -o main.o
g++ -I HTTPClientLib/include BingResultSet.o main.o -o main -L HTTPClientLib/lib/
$ cat .depends
BingResultSet.o: BingResultSet.cpp BingResultSet.h
main.o: main.cpp BingResultSet.h
test.o: test.cpp
¹ (or similar, see man-page) ¹(或类似内容,请参见手册页)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.