gpt4 book ai didi

c++ - 如何在makefile的给定目录中链接opencv lib

转载 作者:行者123 更新时间:2023-12-03 07:03:24 26 4
gpt4 key购买 nike

我正在编译一个项目,我想将其链接到给定目录中的opencv,而不是像/usr/local/lib/libopencv这样的系统目录中
最后是默认的makefile,它链接到系统目录/usr/local/lib
我要链接的opencv位于../../third_party/opencv/目录中,带有../../third_party/opencv/include/../../third_party/opencv/lib/。绝对路径是/home/somepath/third_party/opencv/
如何修改makefile以使项目链接到我要使用的opencv,而不是系统版本?
当前,当我编译项目并使用ldd来检查其依赖性时,说ldd evaluate | grep opencv,我得到了以下内容。这意味着项目仍链接到系统版本,而不是我选择的版本。

libopencv_highgui.so.3.0 => /usr/local/lib/libopencv_highgui.so.3.0 (0x00007efc72527000)
libopencv_imgcodecs.so.3.0 => /usr/local/lib/libopencv_imgcodecs.so.3.0 (0x00007efc7217a000)
libopencv_imgproc.so.3.0 => /usr/local/lib/libopencv_imgproc.so.3.0 (0x00007efc711f8000)
libopencv_core.so.3.0 => /usr/local/lib/libopencv_core.so.3.0 (0x00007efc70565000)
感谢你们对我的帮助!!!
PROJECT_NAME:= evaluate

# config ----------------------------------

INCLUDE_DIRS := include
LIBRARY_DIRS := lib

COMMON_FLAGS := -DCPU_ONLY
CXXFLAGS := -std=c++11 -fopenmp -D_GLIBCXX_USE_CXX11_ABI=0

LDFLAGS := -fopenmp -Wl,-rpath,./lib

OPENCV = `pkg-config opencv --cflags --libs`
LIB = $(OPENCV)
BUILD_DIR := build

# make rules -------------------------------
CXX ?= g++
BUILD_DIR ?= ./build

LIBRARIES += opencv_core opencv_highgui opencv_imgproc #opencv_imgcodecs

CXXFLAGS += $(COMMON_FLAGS) $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(COMMON_FLAGS) $(foreach includedir,$(LIBRARY_DIRS),-L$(includedir)) $(foreach library,$(LIBRARIES),-l$(library))
SRC_DIRS += $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)
CXX_SRCS += $(shell find src/ -name "*.cpp")
CXX_TARGETS:=$(patsubst %.cpp, $(BUILD_DIR)/%.o, $(CXX_SRCS))
ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)))

.PHONY: all
all: $(PROJECT_NAME)

.PHONY: $(ALL_BUILD_DIRS)
$(ALL_BUILD_DIRS):
@mkdir -p $@

$(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
@echo "CXX" $<
@$(CXX) $(CXXFLAGS) -c -o $@ $<

$(PROJECT_NAME): $(CXX_TARGETS)
@echo "CXX/LD" $@
@$(CXX) -o $@ $^ $(LDFLAGS) $(LIB)

.PHONY: clean
clean:
@rm -rf $(CXX_TARGETS)
@rm -rf $(PROJECT_NAME)
@rm -rf $(BUILD_DIR)

最佳答案

一种方法是修改下一行

INCLUDE_DIRS := ../../third_party/opencv/include/ include
LIBRARY_DIRS := ../../third_party/opencv/lib/ lib
或者第二种不同的方法是将 -I-L选项添加到下一行
CXXFLAGS := -std=c++11 -fopenmp -D_GLIBCXX_USE_CXX11_ABI=0 -I../../third_party/opencv/include/
LDFLAGS := -fopenmp -Wl,-rpath,./lib -L../../third_party/opencv/lib/

关于c++ - 如何在makefile的给定目录中链接opencv lib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64673934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com