【问题标题】:How to include header from self compiled library?如何包含自编译库中的头文件?
【发布时间】:2013-10-29 06:29:40
【问题描述】:

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

这是我的 Makefile 链接库:

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 存在于该项目中,而不是这个。 编译这个库的另一个项目的makefile如下:

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)

【问题讨论】:

    标签: c++ c makefile include static-libraries


    【解决方案1】:

    “-L”指定链接器的库路径。

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

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

    【讨论】:

    • 试过了,但它仍然无法编译并出现同样的错误。
    • 好的,那么相对于俄罗斯方块Makefile,simplog.h在“./src”中?
    • 事实上,你的问题听起来好像有两个 Makefile。
    • 好吧,libsimplog.a 在 ./src 中。 simplog.h 存在于它自己的项目目录中,我正在编译 libsimplog.a 并将其复制到该项目中。更新了 OP 以使其更加清晰。
    • 库/链接与包含头文件无关 - 两者完全独立。暂时忘记 .a 文件。在您拥有俄罗斯方块 Makefile 的目录中,您在其中键入“make”但它失败了,您可以执行“ls ./src/simplog.h”吗?还是在别的地方?包含操作将与该目录相关,库的位置与它无关。如果您将 -I./src/ 添加到编译器参数,则 compiler 正在尝试访问文件 ${TETRIS_MAKEFILE_DIR}/src/simplog.h。
    【解决方案2】:

    您是否尝试包含 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多