服务器之家

服务器之家 > 正文

makefile如何调用静态库的方法实现

时间:2021-10-15 14:27     来源/作者:wch_NUDT

makefile如何调用静态库

1、在windows下 g++环境中,调用一个python的静态链接库,在makefile文件有一段是全局的g++命令:

?
1
2
$(PROG): $(OBJS)
 $(CXX) $(LFLAGS) $^ -o $@

现在我要链接上一个python的静态库,lib存储在makefile同目录下libs文件夹下:

?
1
2
$(PROG): $(OBJS)
 $(CXX) $(LFLAGS) $^ -o $@ ./libs/libpython37.a

2、在Linux Ubuntu 18.04版本中添加静态库,视python版本而定:

?
1
2
$(PROG): $(OBJS)
 $(CXX) $(LFLAGS) $^ -o $@ -lpython3.6m

Makefile 如何调用opencv 的静态库

main.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include "opencv2/opencv.hpp"
 
int main()
{
 
  cv::Mat image = cv::imread("./testPic/jianhui.jpg");
 
  //cv::imshow("xxx", image);
 
  //cv::waitKey(40);
 
  printf("image.col=%d image.row=%d \n", image.cols, image.rows);
 
  cv::imwrite("aa.jpg", image);
 
  return 0;
}

Makefile

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
EXE=main
 
#SUBDIR=src object
#CXXFLAGS:编译选项, LDFLAGS:链接选项
thirdparty_root := /local_install/static
CC := g++
 
 
#export PKG_CONFIG_PATH := ${thirdparty_root}/lib/pkgconfig:${PKG_CONFIG_PATH}
#INCS += `pkg-config --cflags opencv`
#LIBS += `pkg-config --libs opencv`
 
INCS := -I${thirdparty_root}/include
 
 
LIBS += -L${thirdparty_root}/lib -lopencv_imgcodecs -lopencv_dnn -lopencv_imgproc -lopencv_core
#LIBS += -L${thirdparty_root}/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core # 亲们,顺序不能变,因为这个快把我整崩溃了
LIBS += -L${thirdparty_root}/share/OpenCV/3rdparty/lib -lIlmImf -llibjasper -llibjpeg -llibpng -llibtiff -llibwebp -lzlib -lippicv -ldl
 
 
 
CXXFLAGS := -Wall -c -O2 -std=c++11 $(INCS)
LDFLAGS := $(LIBS)
LDFLAGS += -lpthread
#LDFLAGS += -Wl,-rpath,${thirdparty_root}/share/OpenCV/3rdparty/lib:${thirdparty_root}/lib: # 动态库的时候需要寻找的路径
 
 
 
#CXX_SOURCES =$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.cpp))
#CXX_OBJECTS=$(patsubst %.cpp, %.o, $(CXX_SOURCES))
 
CXX_OBJECTS := $(patsubst %.cpp,%.o,$(shell find . -name "*.cpp"))
DEP_FILES =$(patsubst %.o, %.d, $(CXX_OBJECTS))
 
$(EXE): $(CXX_OBJECTS)
 $(CC) $(CXX_OBJECTS) -o $(EXE) $(LDFLAGS)
 
%.o: %.cpp
 $(CC) -c $(CXXFLAGS) -MMD $< -o $@
 
-include $(DEP_FILES)
 
clean:
 rm -rf $(CXX_OBJECTS) $(DEP_FILES) $(EXE)
 
test:
 echo $(CXX_OBJECTS)

上面的Makefile 是用调用动态库的方式来调用静态库,如果同时存在静态库和动态库,会优先调用动态库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
EXE=CppWebServer
 
#SUBDIR=src object
#CXXFLAGS:编译选项, LDFLAGS:链接选项
CC := g++
thirdparty_root := /local_install/static
 
 
#INCS += `pkg-config --cflags opencv`
INCS += -I/usr/lib/jdk1.8.0_191/include
INCS += -I/usr/lib/jdk1.8.0_191/include/linux/
 
INCS += -I./include
 
INCS := -I${thirdparty_root}/include
 
# 这种方式调用静态库,保证在在动态库也存在的时候也能调用到静态库
LIBS += ${thirdparty_root}/lib/libopencv_imgcodecs.a
LIBS += ${thirdparty_root}/lib/libopencv_dnn.a
LIBS += ${thirdparty_root}/lib/libopencv_imgproc.a
LIBS += ${thirdparty_root}/lib/libopencv_core.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/libIlmImf.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibjasper.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibjasper.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibjpeg.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibpng.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibtiff.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibwebp.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/libzlib.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/libippicv.a
 
 
LIBS += -L./lib -lTHFaceImage -lTHFeature
LIBS += -ldl
#LIBS += `pkg-config --libs opencv`
 
 
CXXFLAGS := -Wall -c -O2 -std=c++11 $(INCS)
LDFLAGS := $(LIBS)
LDFLAGS += -lpthread
 
#CXXFLAGS += -I/usr/local/include/
#LDFLAGS += -L/usr/local/lib -lopencv_world
 
#CXX_SOURCES =$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.cpp))
#CXX_OBJECTS=$(patsubst %.cpp, %.o, $(CXX_SOURCES))
 
CXX_OBJECTS := $(patsubst %.cpp,%.o,$(shell find . -name "*.cpp"))
CXX_OBJECTS += $(patsubst %.c,%.o,$(shell find . -name "*.c"))
 
DEP_FILES =$(patsubst %.o, %.d, $(CXX_OBJECTS))
 
$(EXE): $(CXX_OBJECTS)
 $(CC) $(CXX_OBJECTS) -o $(EXE) $(LDFLAGS)
 
%.o: %.cpp
 $(CC) -c $(CXXFLAGS) -MMD $< -o $@
 
-include $(DEP_FILES)
 
clean:
 rm -rf $(CXX_OBJECTS) $(DEP_FILES) $(EXE)
 
OBJ_DIR = obj
move:
 if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi;\
 mv $(CXX_OBJECTS) $(OBJ_DIR); \
 mv $(DEP_FILES) $(OBJ_DIR); \
 mv $(EXE) $(OBJ_DIR)
 
test:
 echo $(CXX_OBJECTS)

到此这篇关于makefile如何调用静态库的方法实现的文章就介绍到这了,更多相关makefile调用静态库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wch0lalala/article/details/105373419

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部