【发布时间】:2013-11-23 01:35:31
【问题描述】:
我以前从未尝试过 QT Creator,我想我会为我正在使用的一些测试代码试一试。
长话短说,我无法访问其他 .c 文件中定义的函数。因此,我在 QT Creator 中制作了一个非常简单、愚蠢的程序来尝试查看问题是否仍然存在,并且确实存在。
该程序是一个带有按钮的单个窗口。但是,我的错误可能与按钮完全无关。
我得到的错误是(我知道,这是一个通常非常简单的常见错误):
main.o: In function main':
/home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference todoubler(int)'
基本上我有一个 C 文件,myfuncts.c,看起来像这样:
#include "myfuncts.h"
int doubler(int a){
return a*2;
}
还有头文件myfuncts.h:
#ifndef MYFUNCTS_H
#define MYFUNCTS_H
int doubler(int a);
#endif // MYFUNCTS_H
(到目前为止,我尽可能保持简单,直到我让它工作为止)。
现在在main.cpp 中我只引用了这个函数一次,而这确实是我在编译时出现错误的地方。这里是main.cpp:
#include <QtGui/QApplication>
#include "widget.h"
#include "myfuncts.h"
int main(int argc, char *argv[])
{
int num = 42;
num = doubler(num);
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
当我尝试构建时,这是我的错误:
make: Entering directory /home/user/Documents/dev/st2/st2'
g++ -o st2 main.o widget.o myfuncts.o moc_widget.o -L/usr/lib -lQtGui -lQtCore -lpthread
main.o: In functionmain':
/home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference to doubler(int)'
collect2: ld returned 1 exit status
make: *** [st2] Error 1
make: Leaving directory/home/user/Documents/dev/st2/st2'
Exited with code 2.
Error while building project st2
When executing build step 'Make'
这是项目文件,st2.pro:
# -------------------------------------------------
# Project created by QtCreator 2013-11-22T17:06:59
# -------------------------------------------------
TARGET = st2
TEMPLATE = app
SOURCES += main.cpp \
widget.cpp \
myfuncts.c
HEADERS += widget.h \
myfuncts.h
FORMS += widget.ui
我试图在这里保持惯例,并允许 QT Creator 为我构建项目文件。我还使用“添加新...”菜单来添加 myfunct.h 和 .c 文件,这两个文件都存在于同一目录中。
进入终端并尝试手动生成同样的错误,我发布它以便可以看到错误之前的步骤:
user@singularity:st2$ 制作
/usr/bin/uic-qt4 widget.ui -o ui_widget.h
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I。 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -一世。 -o main.o main.cpp
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I。 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -一世。 -o widget.o widget.cpp
gcc -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I。 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -一世。 -o myfuncts.o myfuncts.c
/usr/bin/moc-qt4 -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -一世。小部件.h -o moc_widget.cpp
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I。 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -一世。 -o moc_widget.o moc_widget.cpp
g++ -o st2 main.o widget.o myfuncts.o moc_widget.o -L/usr/lib -lQtGui -lQtCore -lpthread
main.o:在函数main':
/home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference todoubler(int)'
collect2: ld 返回 1 个退出状态
制作:*** [st2] 错误 1
nm 确实显示了 myfunct.o 中存在的符号:
user@singularity:st2$ nm myfuncts.o 0000000000000000 T倍增器 用户@奇点:st2$
我过去曾用多个库和 c 文件编写过大型项目,没有遇到任何问题,但我不明白 QT Creator 中发生了什么如此不同。似乎一切都在正确的位置并得到说明,但是,这是我第一次使用 QT,所以一定有什么!
任何帮助将不胜感激!平台是 AMD64 上的 Debian Linux 6.0.2。
【问题讨论】: