How to include header from self compiled library?

会有一股神秘感。 提交于 2020-01-06 15:07:19

问题


Okay, so I've compiled a simple logger I wrote into a library so I can include it with other projects. But when I try to include the header for that logger, it fails compilation.

Here is my Makefile linking the library:

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)

The library is libsimplog.a and is located in the src directory of this project. I'm compiling it in a separate project folder, then copying here. simplog.h/simplog.c only exist in the project folder where I'm initially compiling this library. From what I understand, -L ./src/ -lsimplog in my compile flags should be linking this library. However, my #include "simplog.h" is still failing upon compilation:

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

I'm compiling the library in its own project folder, separate from this project. simplog.h/simplog.c exist in that project, not this one. The makefile for the other project that compiles this library is as follows:

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)

回答1:


"-L" specifies the library path for the linker.

You need to tell the pre-processor that there is another include directory with -I./src/.

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



回答2:


Did you try including I Flags for including header files ? eg :

    -I/usr/include/

From the man page of 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.


来源:https://stackoverflow.com/questions/19651222/how-to-include-header-from-self-compiled-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!