我在Windows 7中使用GTK + 3.x和MinWG GCC。我已经设置了所有路径,并且一切都可以用于编译未定义引用错误的位置。
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x7): undefined reference to `gtk_main_quit'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x41): undefined reference to `gtk_init_abi_check'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x4d): undefined reference to `gtk_window_new'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x85): undefined reference to `g_signal_connect_data'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x91): undefined reference to `gtk_label_new'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0x9a): undefined reference to `gtk_container_get_type'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0xae): undefined reference to `g_type_check_instance_cast'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0xbe): undefined reference to `gtk_container_add'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0xca): undefined reference to `gtk_widget_show'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0xd6): undefined reference to `gtk_widget_show'
C:\Users\user\AppData\Local\Temp\cc8N3tch.o:helloworld.cpp:(.text+0xdb): undefined reference to `gtk_main'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\cc8N3tch.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
使用GTK网站上的示例代码
#include <gtk/gtk.h>
static void on_window_closed(GtkWidget * widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char * argv[])
{
GtkWidget * window, * label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect( window, "destroy", G_CALLBACK(on_window_closed), NULL);
label = gtk_label_new("Hello, World!");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show(label);
gtk_widget_show(window);
gtk_main();
return 0;
}
我用来构建它的批处理文件
@echo off
set gtk_ver=gtk+-3.0
pkg-config %gtk_ver% --cflags --libs >tmp.txt
set /p pkg-info= <tmp.txt
del tmp.txt
rem echo &pkg-info%
gcc -o helloworld helloworld.cpp %pkg-info%
这是tmp.txt文件在删除之前的样子
-mms-bitfields -IC:/gtk/include/gtk-3.0 -IC:/gtk/include/cairo -IC:/gtk/include/pango-1.0 -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/pixman-1 -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include -IC:/gtk/include/libpng15 -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/libpng15 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -LC:/gtk/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
我尝试过编辑批处理以不同的方式运行gcc,但都失败了。
gcc %pkg-info% helloworld.cpp -o helloworld
gcc helloworld.cpp -o %pkg-info% helloworld //Gives me helloworld: no such file or directory
gcc helloworld -o %pkg-info% helloworld.cpp //Gives me helloworld: no such file or directory
gcc helloworld.cpp %pkg-info% -o helloworld
我使用批处理文件的原因是因为使用`pkg-config --libs --cflags gtk+-3.0`
失败,导致pkg-config不存在的错误。在cl上运行pkg-config,而不是使用gcc,运行正常。
来自Mofi批处理文件的pk-config。
-mms-bitfields -IC:\gtk\include\gtk-3.0 -IC:\gtk\include\cairo -IC:\gtk\include\pango-1.0 -IC:\gtk\include\atk-1.0 -IC:\gtk\include\cairo -IC:\gtk\include\pixman-1 -IC:\gtk\include -IC:\gtk\include\freetype2 -IC:\gtk\include -IC:\gtk\include\libpng15 -IC:\gtk\include\gdk-pixbuf-2.0 -IC:\gtk\include\libpng15 -IC:\gtk\include\glib-2.0 -IC:\gtk\lib\glib-2.0\include -LC:\gtk\lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
Mofi的包含完整路径的批处理文件
@echo off
rem Get all options for build into an environment variable.
set "gtk_ver=gtk+-3.0"
"C:\gtk\bin\pkg-config.exe" %gtk_ver% --cflags --libs >"%TEMP%\gtk_options.tmp"
set /p pkg-info=<"%TEMP%\gtk_options.tmp"
rem Replace all / by \ in the environment variable.
set "pkg-info=%pkg-info:/=\%"
del %TEMP%\gtk_options.tmp
rem echo %pkg-info%
"C:\MinGW\bin\gcc.exe" %pkg-info% -o helloworld helloworld.c
PAUSE
您可以download gtk目录的文本文件。 (对不起,如果它充满广告。这是一个免费的文件丢弃位置。未知它将持续多长时间。将在它被删除时更新)
答案 0 :(得分:0)
我不知道下面的批处理文件代码是否有助于解决此问题,因为我无法访问您的计算机并查看目录和文件。但试一试。
@echo off
rem Get all options for build into an environment variable.
set "gtk_ver=gtk+-3.0"
"C:\gtk\bin\pkg-config.exe" %gtk_ver% --cflags --libs >"%TEMP%\gtk_options.tmp"
set /p pkg-info=<"%TEMP%\gtk_options.tmp"
del "%TEMP%\gtk_options.tmp"
rem Replace all / by \ in the environment variable.
set "pkg-info=%pkg-info:/=\%"
rem echo %pkg-info%
"C:\MinGW\bin\gcc.exe" helloworld.c -o helloworld %pkg-info%
修改强>
我在最后一行调用GCC时更改了顺序。订单现在是
此修改的灵感来自Problem with compiling GTK3 program。
我将源文件重命名为 helloworld.c ,因为该文件包含C而不是C ++代码。文件扩展名 cpp 会导致文件自动编译为C ++文件。如果 gtk.h 不包含
,则可能导致链接器错误#ifdef __cplusplus
extern "C" {
#endif
/* Function declarations */
#ifdef __cplusplus
}
#endif
答案 1 :(得分:0)
我没有看到-lgtk-win32-3.0
标志! ,我认为你正在使用gtk +的x64包,
如果您正在运行x64 windows下载x32捆绑的gtk +而不是x64,
祝你好运,