使用 Makefile 结合其他库编译 gtk 库

Compile gtk library in combination with other libraries using a Makefile

我在用我的 C++ 程序编译 gtk 库时遇到问题。我安装了 gtk 并且我的程序在使用如下终端命令时正在运行:

gcc -Wall -g helloworld.c -o helloworld `gtk-config --cflags` \
    `gtk-config --libs` 

但我需要它与其他一些库一起编译,所以我有一个 Makefile,但不知道如何让它也编译 gtk 库。

CFLAGS = -g -Wall -fPIC -Wextra -Werror -lpthread -pthread # for Linux and other gcc systems
OP=$(CFLAGS)  
CC=g++  #for Linux

# compilation rule for general cases
.o :
    $(CC) $(OP) -o $@ $? -lm
.c.o:
    $(CC) -c $(OP) $<     

SWEOBJ = swedate.o swehouse.o swejpl.o swemmoon.o swemplan.o swepcalc.o sweph.o\
    swepdate.o swephlib.o swecl.o swehel.o

astrogtk: astrogtk.o libswe.a
    $(CC) $(OP) -I/home/arjan/astroproject -o astrogtk astrogtk.o -L. -lswe -lm -ldl 

swemini: swemini.o libswe.a
    $(CC) $(OP) -o swemini swemini.o -L. -lswe -lm

# create an archive and a dynamic link libary fro SwissEph
# a user of this library will inlcude swephexp.h  and link with -lswe

libswe.a: $(SWEOBJ)
    ar r libswe.a   $(SWEOBJ)

libswe.so: $(SWEOBJ)
    $(CC) -shared -o libswe.so $(SWEOBJ)

clean:
    rm -f *.o astrogtk libswe*

###
swecl.o: swejpl.h sweodef.h swephexp.h swedll.h sweph.h swephlib.h
sweclips.o: sweodef.h swephexp.h swedll.h
swedate.o: swephexp.h sweodef.h swedll.h
swehel.o: swephexp.h sweodef.h swedll.h
swehouse.o: swephexp.h sweodef.h swedll.h swephlib.h swehouse.h
swejpl.o: swephexp.h sweodef.h swedll.h sweph.h swejpl.h
swemini.o: swephexp.h sweodef.h swedll.h
swemmoon.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h
swemplan.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h swemptab.h
swepcalc.o: swepcalc.h swephexp.h sweodef.h swedll.h
sweph.o: swejpl.h sweodef.h swephexp.h swedll.h sweph.h swephlib.h
swephlib.o: swephexp.h sweodef.h swedll.h sweph.h swephlib.h
astrogtk.o: swephexp.h sweodef.h swedll.h astromath.h

我尝试在 Makefile 中的不同位置添加 gtk 行

gtk-config --cflags gtk-config --libs

但是无论我把它放在哪里,它都说找不到 gtk 头文件...

astrogtk.cpp:1:21: fatal error: gtk/gtk.h: No such file or directory
compilation terminated.
<builtin>: recipe for target 'astrogtk.o' failed
make: *** [astrogtk.o] Error 1

那么使用这个 makefile 编译 gtk 库的解决方案是什么?

只需将 gtk-config --cflags 添加到您的 CFLAGS。然后将 gtk-config --libs 添加到您的库中,就在 -lswe -lm -ldl

旁边