Eclipse和Gtkmm - “未定义引用”

时间:2011-12-13 17:23:42

标签: c++ eclipse-cdt gtkmm

我使用Eclipse,我想在其中使用gtkmm。我有以下代码:

#include <gtkmm.h>
#include <iostream>

int main(int argc, char *argv[]) {
    Gtk::Main kit(argc, argv);

    Gtk::Window mainWindow;

    Gtk::Button button("Click here");

    mainWindow.set_title("Eclipse/GTKmm Demo");
    mainWindow.set_border_width(4);
    mainWindow.set_default_size(200, 50);

    mainWindow.add(button);
    button.show();

    Gtk::Main::run(mainWindow);

    return 0;
}

我添加了pkg-config --cflags --libs gtkmm-3.0(当然有严重的口音)将Cross G ++ Compiler Miscellanous选项添加到Other标记中,并将Cross G ++ Compiler Miscellanous选项添加到Linker标记中。它不起作用!

这是编译日志:

**** Build of configuration Debug for project User Directory Changer ****

make all 
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 `pkg-config --cflags --libs gtkmm-3.0` -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: User Directory Changer
Invoking: Cross G++ Linker
g++ `pkg-config --cflags --libs gtkmm-3.0` -o "User Directory Changer"  ./main.o   
./main.o: In function `main':
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:12: undefined reference to `Gtk::Main::Main(int&, char**&, bool)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:14: undefined reference to `Gtk::Window::Window(Gtk::WindowType)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Glib::ustring::ustring(char const*)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Gtk::Button::Button(Glib::ustring const&, bool)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Glib::ustring::~ustring()'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:18: undefined reference to `Glib::ustring::ustring(char const*)'
[etc...]
collect2: ld returned 1 exit status
make: *** [User Directory Changer] Error 1

**** Build Finished ****

我不知道为什么......当我在终端编译它时:g++ -O0 -g3 -Wall -c -fmessage-length=0 'pkg-config --cflags --libs gtkmm-3.0' -o ./test ./main.cpp它可以工作......

2 个答案:

答案 0 :(得分:12)

我找到了一个解决方案: 在链接器选项中,在命令行模式中,我将${FLAGS}移到了末尾,例如:

之前:${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}

之后:${COMMAND} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} ${FLAGS}

现在它有效。

答案 1 :(得分:1)

您必须将pkg-config --cflags <etc>添加到现在的位置(编译器选项),然后将pkg-config --libs <etc>添加到链接器选项