使用 Makefile 与其他库组合编译 gtk 库

Compile gtk library in combination with other libraries using a Makefile

本文关键字:组合 gtk 编译 其他 Makefile 使用      更新时间:2023-10-16

我在用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

我尝试在生成文件内的不同位置添加 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

那么使用此生成文件编译 gtk 库的解决方案是什么?

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

经过修补,我在这里的这个网站上找到了这个命令,它解决了我在 Arch 上使用 c++ 的编译问题:

$ gcc -o simple simple.c `pkg-config --libs --cflags gtk+-3.0` 

可能需要使命令适应您的 GTK 版本。

使用 VS 代码作为 IDE 对导入库有很大帮助。