2012-07-02 3 views
0

C++ Boost 라이브러리를 사용하여 Kinect를 직렬 포트에 연결하여 깊이 데이터를 작성하려고합니다. 이것을하기 위해서는이 미리 작성된 Makefile에 적절한 라이브러리를 링크해야합니다. boost/asio를 사용하여 라이브러리에 대한 느낌을 얻고 컴파일하기 위해/usr/local/lib에있는 boost_system 라이브러리에 링크하고/usr/local/include/lib 디렉토리에있는 헤더 파일을 포함시켜야한다는 기본 프로그램을 작성했습니다. 후원. 내 OpenNI 코드에서 대신이 Makefile과 동일한 연결을 만들어야한다고 생각했다.OpenNI 메이크 파일로 Boost :: 시스템 라이브러리에 연결하기

# 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 

내가 추가 한 다음 INC_DIRS_OPTION LIBS_DIRS_OPTION에

-L/usr/local/lib 

-I/usr/local/include/boost 

-lboost_system 

USED_LIBS_OPTION (ASIO 부스트 시스템 라이브러리에 의존에 오류 gen 나는 아무것도하지 않았다 내 DYLD_LIBRARY_PATH에 lib 디렉토리를 추가하려고했습니다

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 

: 어떤 종류의 eration)

나는이 오류가 발생합니다. 나는 또한 libboost_system.a 파일과 결합 된 NiSimpleRead.o 바이너리를 사용하여 일종의 공유 (* .so) 라이브러리를 만들려고했는데, 많은 오류가 발생했습니다. 그런 다음 boost_system.a 파일을 자체적으로 .so로 변경해 보았습니다. 이것에 대한 조언은 훌륭 할 것입니다.

답변

0

문제는 64 비트 부스트 만 있고 유니버설 (32 비트 및 64 비트 모두) 바이너리를 만들려고하는 것 같습니다. 32 비트 및 64 비트 버전 모두에 종속성을 설치하거나 빌드 지침에서이 부분을 버리십시오. -arch i386

+0

답장을 보내 주셔서 감사합니다. 나는 그 중 어떤 것에 대해서 어떻게 가야하는지 잘 모르겠다. 나는 32 비트 종속성을 찾지 못한다. 또한 빌드 지시에서 -arch i386을 제거하는 방법을 알아낼 수 있는가? – maz

+0

32 비트 Ubuntu 11.04 랩톱으로 모든 것을 전환하고 Boost를 설치하면 모든 것이 잘 컴파일됩니다. 따라서 올바른 의존성을 갖는 문제였습니다. 건배 – maz