如何包含自编译库中的标头

How to include header from self compiled library?

本文关键字:何包含 编译      更新时间:2023-10-16

好的,所以我编译了一个简单的记录器,我写到一个库中,这样我就可以把它包含在其他项目中。 但是当我尝试包含该记录器的标头时,编译失败。

这是我链接库的制作文件:

CC= clang++
PROG= ./bin/tetris
OBJS= ./src/main.o ./src/Tetris.o ./src/states/BaseState.o ./src/states/MenuState.o 
    ./src/states/GameState.o
LIBS= allegro-5.0 allegro_dialog-5.0 allegro_font-5.0 allegro_ttf-5.0 allegro_color-5.0
CXXFLAGS= -g -Wall -std=c++11
LDFLAGS= $(shell pkg-config --static --libs ${LIBS}) -I./src/util -L./src/util/ -lsimplog
all: $(PROG)
$(PROG): $(OBJS)
    mkdir -p ./bin/
    $(CC) -o $(PROG) $(CXXFLAGS) $(OBJS) $(LDFLAGS)
    rm -f $(OBJS)
clean:
    rm -f $(PROG) $(TEST_PROG) $(OBJS)

该库libsimplog.a,位于该项目的src目录中。 我正在一个单独的项目文件夹中编译它,然后复制到这里。simplog.h/simplog.c 只存在于我最初编译此库的项目文件夹中。 据我了解,我的编译标志中的-L ./src/ -lsimplog应该链接这个库。但是,我的#include "simplog.h"在编译时仍然失败:

fatal error: simplog.h: No such file or directory

我正在自己的项目文件夹中编译库,与此项目分开。simplog.h/simplog.c 存在于该项目中,而不是这个项目。编译此库的其他项目的生成文件如下所示:

CC = clang
CFLAGS = -Wall -c
LIB = libsimplog.a
SOURCE = simplog.c
OBJ = simplog.o
all:
    $(CC) $(CFLAGS) $(SOURCE)
    ar -cvq $(LIB) $(OBJ)
clean:
    rm -f $(LIB)

"-L" 指定链接器的路径。

您需要告诉预处理器还有另一个包含 -I./src/的包含目录。

CXXFLAGS= -g -Wall -std=c++11 -I./src/ -L./src/ -lsimplog $(shell pkg-config --cflags ${LIBS})

您是否尝试过包含 I 标志以包含头文件?例如:

    -I/usr/include/

来自 g++ 的手册页

   -I dir           Add the directory dir to the list of directories to 
be searched for header files.  Directories named by -I are searched  
before the standard system include directories. 
If the directory dir is a standard system include directory, 
the option is ignored to ensure that the default search order for
system directories and the special treatment of system headers are no   
defeated .If dir begins with "=", then the "=" will be replaced by the
sysroot prefix; see --sysroot and -isysroot.