简体   繁体   中英

How to properly compile and link against C program?

I have a logger that I wrote in C, and have included it in a C++ project I am working on. It works fine, but I get a deprecated warning from Clang++: "Treating C input as C++ when in C++ mode, this behaviour is deprecated."

In hopes of doing away with this warning, I moved the compilation of the logger into its own step in the make file and compiled the object file with Clang. Then I added a reference to that object file to my Clang++ rule to link against. Now it fails to link properly: "Linker command failed with exit code 1"

My makefile is below. How would I properly go about compiling and linking this C logger so as to do away with that warning in Clang++?

CC= clang++
PROG= ./bin/tetris
OBJS= ./src/main.o ./src/Tetris.o ./src/states/BaseState.o ./src/states/MenuState.o \
    ./src/states/GameState.o ./src/entities/Block.o ./src/entities/Tetromino.o \
    ./src/entities/Grid.o
LIBS= allegro-5.0 allegro_dialog-5.0 allegro_font-5.0 allegro_ttf-5.0 allegro_color-5.0 \
    allegro_primitives-5.0 allegro_main-5.0 allegro_image-5.0 allegro_audio-5.0 allegro_memfile-5.0
CXXFLAGS= -g -Wall -std=c++11 $(shell pkg-config --cflags ${LIBS})
LDFLAGS= $(shell pkg-config --static --libs ${LIBS})

all: logger $(PROG)

$(PROG): $(OBJS)
    mkdir -p ./bin
    $(CC) -v -o $(PROG) $(LDFLAGS) $(OBJS) ./src/util/SimpleLogger/simplog.o
    rm -f $(OBJS)

logger:
    clang -c -Wall ./src/util/SimpleLogger/simplog.c -o ./src/util/SimpleLogger/simplog.o

clean:
    rm -f $(PROG) $(OBJS)

This may not be what you are looking for, but you could include it in your C++ file by:

#ifdef __cplusplus
extern "C" {
#endif

// C code

#ifdef __cplusplus
}
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM