我想使用C++ Boost庫來寫一些深度數據,我形成我的Kinect到串行端口。爲了做到這一點,我需要將相應的庫與這個預先編寫的Makefile鏈接起來。我使用boost/asio編寫了一個基本程序來感受這些庫,爲了進行編譯,我必須鏈接到位於/ usr/local/lib的boost_system庫,並將頭文件包含在/ usr/local/include /促進。我想在我的OpenNI代碼中使用它,我只需要從該Makefile創建相同的連接。鏈接升壓::系統庫與OpenNI生成文件
# take this file's dir
COMMON_CPP_MAKE_FILE_DIR = $(dir $(lastword $(MAKEFILE_LIST)))
include $(COMMON_CPP_MAKE_FILE_DIR)CommonDefs.mak
# define a function to figure .o file for each source file (placed under intermediate directory)
SRC_TO_OBJ = $(addprefix ./$(INT_DIR)/,$(addsuffix .o,$(notdir $(basename $1))))
# create a list of all object files
OBJ_FILES = $(call SRC_TO_OBJ,$(SRC_FILES_LIST))
# define a function to translate any source file to its dependency file (note that the way we create
# dep files, as a side affect of compilation, always puts the files in the INT_DIR with suffix .d)
SRC_TO_DEP = $(addprefix ./$(INT_DIR)/,$(addsuffix .d,$(notdir $(basename $1))))
# create a list of all dependency files
DEP_FILES = $(call SRC_TO_DEP,$(SRC_FILES_LIST))
# older version of gcc doesn't support the '=' symbol in include dirs, so we replace it ourselves with sysroot
INC_DIRS_FROM_SYSROOT = $(patsubst =/%,$(TARGET_SYS_ROOT)/%,$(INC_DIRS))
# append the -I switch to each include directory
INC_DIRS_OPTION = $(foreach dir,$(INC_DIRS_FROM_SYSROOT),-I$(dir)) -I/usr/local/include/boost
# append the -L switch to each library directory
LIB_DIRS_OPTION = $(foreach dir,$(LIB_DIRS),-L$(dir)) -L$(OUT_DIR) -L/usr/local/lib
# append the -l switch to each library used
USED_LIBS_OPTION = $(foreach lib,$(USED_LIBS),-l$(lib)) -lboost_system
# append the -D switch to each define
DEFINES_OPTION = $(foreach def,$(DEFINES),-D$(def))
# tell compiler to use the target system root
ifdef TARGET_SYS_ROOT
CFLAGS += --sysroot=$(TARGET_SYS_ROOT)
LDFLAGS += --sysroot=$(TARGET_SYS_ROOT)
endif
# set Debug/Release flags
ifeq "$(CFG)" "Debug"
CFLAGS += -O0 -g
endif
ifeq "$(CFG)" "Release"
CFLAGS += -O2 -DNDEBUG
endif
CFLAGS += $(INC_DIRS_OPTION) $(DEFINES_OPTION)
LDFLAGS += $(LIB_DIRS_OPTION) $(USED_LIBS_OPTION)
# some lib/exe specifics
ifneq "$(LIB_NAME)" ""
OUTPUT_NAME = lib$(LIB_NAME).so
CFLAGS += -fPIC -fvisibility=hidden
ifneq ("$(OSTYPE)","Darwin")
LDFLAGS += -Wl,--no-undefined
OUTPUT_NAME = lib$(LIB_NAME).so
OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS) -shared
else
LDFLAGS += -undefined error
OUTPUT_NAME = lib$(LIB_NAME).dylib
OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS) -dynamiclib -headerpad_max_install_names
endif
endif
ifneq "$(EXE_NAME)" ""
OUTPUT_NAME = $(EXE_NAME)
OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS)
endif
ifneq "$(SLIB_NAME)" ""
CFLAGS += -fPIC
OUTPUT_NAME = lib$(SLIB_NAME).a
OUTPUT_COMMAND = $(AR) -cq $(OUTPUT_FILE) $(OBJ_FILES)
endif
define CREATE_SRC_TARGETS
# create a target for the object file (the CXX command creates both an .o file
# and a .d file)
ifneq ("$(OSTYPE)","Darwin")
$(call SRC_TO_OBJ,$1) : $1 | $(INT_DIR)
$(CXX) -MD -MP -MT "$(call SRC_TO_DEP,$1) [email protected]" -c $(CFLAGS) -o [email protected] $$<
else
$(call SRC_TO_OBJ,$1) : $1 | $(INT_DIR)
$(CXX) -c $(CFLAGS) -o [email protected] $$<
endif
endef
#############################################################################
# Targets
#############################################################################
.PHONY: clean-objs clean-defs
include $(COMMON_CPP_MAKE_FILE_DIR)CommonTargets.mak
# create targets for each source file
$(foreach src,$(SRC_FILES_LIST),$(eval $(call CREATE_SRC_TARGETS,$(src))))
# include all dependency files (we don't need them the first time, so we can use -include)
-include $(DEP_FILES)
$(OUTPUT_FILE): $(OBJ_FILES)
$(OUTPUT_COMMAND)
clean-objs:
rm -rf $(OBJ_FILES)
clean-defs:
rm -rf $(DEP_FILES)
clean: clean-objs clean-defs
我說:
-I/usr/local/include/boost
到INC_DIRS_OPTION
-L/usr/local/lib
到LIBS_DIRS_OPTION,並
-lboost_system
到USED_LIBS_OPTION(ASIO依靠升壓系統庫對於錯誤gen某種關合作)
我得到這個錯誤:
c++ -o ../Bin/x64-Release/Sample-NiSimpleRead ./x64-Release/NiSimpleRead.o -arch i386 -arch x86_64 -L../../Lib -L../Bin/x64-Release -lOpenNI -lboost_system
ld: warning: ignoring file /usr/local/lib/libboost_system.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols for architecture i386:
"boost::system::generic_category()", referenced from:
__GLOBAL__I_a in NiSimpleRead.o
"boost::system::system_category()", referenced from:
__GLOBAL__I_a in NiSimpleRead.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [../Bin/x64-Release/Sample-NiSimpleRead] Error 1
我試着加入lib目錄到我的DYLD_LIBRARY_PATH這什麼也沒做。我也嘗試創建一些共享(* .so)庫,首先使用NiSimpleRead.o二進制文件與libboost_system.a文件結合,這引起了很多錯誤。然後嘗試將boost_system.a文件本身更改爲.so,這也沒有做任何事情。任何關於此的建議都會很棒。
非常感謝您的回覆!我真的不知道如何去做這些事情,我找不到32位依賴關係來提升,也不知道如何從構建指令中刪除-arch i386,那裏會有哪些內容? – maz
我將所有內容都切換到了我的32位Ubuntu 11.04筆記本電腦上,並安裝了Boost,一切正常。所以這是一個正確的依賴問題。乾杯 – maz