如何在C++中包含外部库

How to include external library in C++?

本文关键字:包含外 C++      更新时间:2023-10-16

我正在尝试将一个名为SVL(简单向量库)的外部库包含到c++中。我在这方面遇到了很多麻烦。我使用的是Mac OSX,库的README只有适用于Windows的教程。

我做了

 make clean
 make  

这创建了库文件libsvl.alibsvl.dbg.a。我看到,在文件的README中,它说要将其作为svl/vec3.h包括在内,大约用于不同的头。但当我尝试这样做时,它不起作用,仍然说,

vertex.hh:9:10: fatal error: 'svl/Vec3.h' file not found
#include <svl/Vec3.h>
     ^
1 error generated.
vertex.cc:10:10: fatal error: 'svl/Vec3.h' file not found
#include <svl/Vec3.h>  

你能告诉我我缺了什么吗?我可能需要更好地定义通往图书馆的道路,或者做一些不同的事情。

库文件与其他代码的相对路径为../svl-1.5/lib/libsvl.a

使配置文件

#
# Mac OSX
#
CONFIG = OSX
# --- Compilation
----------------------------------------------------------
CC          =   cc
CXX         =   c++
LD          =   c++
MAKEDEP     =   c++ -MM $(SYS_INC_DIRS) $(INC_DIRS) $(SRCS) >     Makefile.depend
CPP         =   cpp
LIBTOOL     =   glibtool
LD_FLAGS    =
CFLAGS      =   -O2
DBG_CFLAGS  =   -g
SHARED_LIBS = off
USE_RANLIB = 1    
DEST        =   $(REACTOR)
config:
    @echo "configured for Mac OSX"
# --- System     -------------------------------------------------------------------
SYS_INC_DIRS        =  -I/sw/include
SYS_LIB_DIRS        =  -L/sw/lib
SYS_LIBS            =
SYS_DBG_LIBS        =
# --- VL     -----------------------------------------------------------------------
CL_EXT_FLAGS = -DCL_TMPL_INST -DCL_HAS_VSNPRINTF -DCL_POSIX_TIME
VL_EXT_FLAGS = -DVL_HAS_ABSF -DVL_USE_MEMCPY

运行make 的结果

cd src && /Applications/Xcode.app/Contents/Developer/usr/bin/make REACTOR=..
c++  -O2   
            -c LibSVL.cpp -I../include -I/sw/include
c++  -O2   
            -c Basics.cpp -I../include -I/sw/include
ar rcu libsvl.a LibSVL.o Basics.o
mkdir -p ../lib
mv libsvl.a ../lib
c++ -DDEBUG  -g  
            -DVL_DEBUG 
            -c LibSVL.cpp -o LibSVL.do -I../include -I/sw/include
c++ -DDEBUG  -g  
            -DVL_DEBUG 
            -c Basics.cpp -o Basics.do -I../include -I/sw/include

完成make文件

#
# Main makefile for SVL distribution
#
# NOTE: To build multiple system libraries in AFS, make 'afs-setup' first.
# This will create the necessary architecture-dependent lib directories,
# and ensure that the config header goes there.
REACTOR=.
all: $(REACTOR)/makefiles/config.mf
    cd src && $(MAKE) REACTOR=..
SHELL           = /bin/sh
DEST            = /usr/local
CONFIG_DEST     = $(DEST)/include
clean:  
    @-echo 'Making clean...'
    @-cd src && $(MAKE) REACTOR=.. clean
    @-rm -f lib/lib*
install: install-headers install-libs
install-headers:
    @-echo "installing into $(DEST)/include/svl $(DEST)/doc"
    @-chmod a+r include/svl/* doc/*
    @-umask 022 
        && mkdir -p $(DEST)/doc $(DEST)/include/svl
    @-umask 022 
        && cp doc/* $(DEST)/doc 
        && cp include/svl/* $(DEST)/include/svl
    @-echo "done."
install-libs:
    @-echo "installing into $(DEST)/lib $(CONFIG_DEST)"
    @-chmod a+r lib/* include/SVLConfig.h
    @-umask 022 
        && mkdir -p $(DEST)/lib $(CONFIG_DEST)
    @-umask 022 
        && cp lib/* $(DEST)/lib 
        && cp include/SVLConfig.h $(CONFIG_DEST)
    @-echo "done."
# setup for multi-architecture build in afs.
afs-setup:
    @-echo "Creating afs multiple-system lib directory"
    @-rm -r lib; ln -s .arch/@sys/lib lib
    # ensure the config file goes in the lib directory.
    @-ln -sf ../lib/svl/SVLConfig.h include/SVLConfig.h
# if lib is a broken symbolic link, create the necessary .arch directory.
afs-lib:
    @-if test ! -d lib; then mkdir -p .arch/`sys`/lib/svl; 
       touch .arch/`sys`/lib/svl/SVLConfig.h; fi
config: afs-lib
    $(MAKE) config -f makefiles/svl.mf
# --- setup -----------
# auto-generated by make-config-mf
$(REACTOR)/makefiles/config.mf:
    @-echo "=== System Configuration ==="
    @-echo "Select one of the following systems, and then run 'make <system>'."
    @-echo 
    @-echo "    OSX"
    @-echo "    alpha"
    @-echo "    linux_RH"
    @-echo "    sgi-n32"
    @-echo "    sgi-n64"
    @-echo "    sgi-o32"
    @-echo "    solaris-gcc"
    @-echo "    sunos-gcc"
    @-echo 
    @-echo "After this is done, you can edit makefiles/config.mf to change"
    @-echo "compiler settings and paths. If you change the build flags, you"
    @-echo "must run 'make config' to update the headers."
    @exit 1
OSX:
    cp $(REACTOR)/makefiles/config-OSX.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
alpha:
    cp $(REACTOR)/makefiles/config-alpha.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
linux_RH:
    cp $(REACTOR)/makefiles/config-linux_RH.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
sgi-n32:
    cp $(REACTOR)/makefiles/config-sgi-n32.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
sgi-n64:
    cp $(REACTOR)/makefiles/config-sgi-n64.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
sgi-o32:
    cp $(REACTOR)/makefiles/config-sgi-o32.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
solaris-gcc:
    cp $(REACTOR)/makefiles/config-solaris-gcc.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config
sunos-gcc:
    cp $(REACTOR)/makefiles/config-sunos-gcc.mf $(REACTOR)/makefiles/config.mf
    $(MAKE) config

您必须执行

sudo make install