1. 程式人生 > 程式設計 >makefile如何呼叫靜態庫的方法實現

makefile如何呼叫靜態庫的方法實現

makefile如何呼叫靜態庫

1、在windows下 g++環境中,呼叫一個python的靜態連結庫,在makefile檔案有一段是全域性的g++命令:

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

現在我要連結上一個python的靜態庫,lib儲存在makefile同目錄下libs資料夾下:

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

2、在Linux Ubuntu 18.04版本中新增靜態庫,視python版本而定:

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

Makefile 如何呼叫opencv 的靜態庫

main.cpp

#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

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,$(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 是用呼叫動態庫的方式來呼叫靜態庫,如果同時存在靜態庫和動態庫,會優先呼叫動態庫

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,$(shell find . -name "*.cpp"))
CXX_OBJECTS += $(patsubst %.c,$(shell find . -name "*.c"))

DEP_FILES =$(patsubst %.o,$(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呼叫靜態庫內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!