简体   繁体   English

QT Creator:无法在QT Creator c项目中包含来自其他文件的功能

[英]QT Creator: Cannot include functions from other files in QT Creator c project

I have never tried QT Creator before and thought I'd give it a spin for some test code I am using. 我以前从未尝试过QT Creator,并且以为我会试用一下我正在使用的一些测试代码。

Long story short, I was unable to access functions defined in my other .c files. 长话短说,我无法访问其他.c文件中定义的功能。 So I made a very simple, stupid program in QT Creator to try and see if the problem persisted, and it does. 因此,我在QT Creator中制作了一个非常简单,愚蠢的程序,尝试查看问题是否仍然存在,并且确实如此。

The program is a single window with a button. 该程序是带有按钮的单个窗口。 However, my error is probably totally irrelevant to the button. 但是,我的错误可能与按钮完全无关。

The error I am getting is (I know, this is a common one that is usually very simple): main.o: In function main': /home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference to doubler(int)' 我得到的错误是(我知道,这是一个通常很简单的常见错误): main.o: In function main': /home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference to doubler(int)'

Basically I have a C file, myfuncts.c that looks like this: 基本上我有一个C文件, myfuncts.c看起来像这样:

#include "myfuncts.h"

int doubler(int a){
return a*2;
}

And the header file myfuncts.h: 和头文件myfuncts.h:

#ifndef MYFUNCTS_H
#define MYFUNCTS_H

int doubler(int a);


#endif // MYFUNCTS_H

(so far I am keeping it as simple as I can until I get it working). (到目前为止,我一直在尽可能地简化它,直到使它起作用为止)。

Now in main.cpp I have referenced this function only once, and this is indeed where the error comes up while I am compiling. 现在在main.cpp我仅引用了此函数一次,这确实是我编译时出现错误的地方。 Here is 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();
}

When I try and build, here is my error: 当我尝试构建时,这是我的错误:

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 function main': /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'

Here is the project file, st2.pro: 这是项目文件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

I am trying to keep with convention here and have allowed QT Creator to build the project file for me. 我试图在这里保持惯例,并允许QT Creator为我构建项目文件。 I have also used the "Add new..." menu to add the myfunct.h and .c files, both of which exist in the same directory. 我还使用了“添加新...”菜单来添加myfunct.h和.c文件,这两个文件都存在于同一目录中。

Dropping into the terminal and trying to make manually results in the same error, I am posting it so that the steps prior to the error can be seen: 放入终端并尝试手动进行操作会导致相同的错误,我正在发布该错误,以便可以看到该错误之前的步骤:

user@singularity:st2$ make /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. -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. -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. -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. -I. widget.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. -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: In function main': /home/user/Documents/dev/st2/st2/main.cpp:9: undefined reference to doubler(int)' collect2: ld returned 1 exit status make: *** [st2] Error 1

And nm does show the symbols exist in myfunct.o: nm确实显示了myfunct.o中存在的符号:

user@singularity:st2$ nm myfuncts.o 0000000000000000 T doubler user@singularity:st2$

I have coded huge projects in the past with multiple libraries and c files without trouble, and I don't understand what is happening in QT Creator that is so different. 过去,我使用多个库和c文件编写了大型项目,而没有遇到麻烦,而且我不了解QT Creator中正在发生的变化如此之大。 It seems like everything is in the right place and accounted for, but, this is my first time with QT so there's got to be something! 似乎一切都在正确的位置并得到了考虑,但是,这是我第一次参加QT,所以肯定有事情!

Any help would be greatly appreciated! 任何帮助将不胜感激! Platform is Debian Linux 6.0.2 on AMD64. 平台是AMD64上的Debian Linux 6.0.2。

Add to your .pro file the following: 将以下内容添加到您的.pro文件中:

INCLUDEPATH += dir/where/is/myfuncts.h

EDIT: And try adding extern "C": 编辑:并尝试添加extern“ C”:

#ifndef MYFUNCTS_H
#define MYFUNCTS_H

/* Extern "C" */
#ifdef __cplusplus
extern "C" {
#endif


int doubler(int a);


/* Extern "C" */
#ifdef __cplusplus
}
#endif


#endif // MYFUNCTS_H

This is a problem about "name mangled". 这是有关“名称混乱”的问题。 If you want to call a C function in the C++, you shall declare the C function with extern "C" in the C++ file. 如果要在C ++中调用C函数,则应在C ++文件中用extern "C"声明C函数。

/* file: main.cpp */
...
extern "C" int doubler(int a);
...

Usually, we modify the C header file so it's easier to #include in C++ code. 通常,我们修改C头文件,以便在C ++代码中更容易#include So, you can do it like this, 所以你可以这样

/ * file: myfuncts.h */
#ifndef MYFUNCTS_H
#define MYFUNCTS_H

#ifdef __cplusplus
extern "C" {
#endif

int doubler(int a);

#ifdef __cplusplus
}
#endif

#endif // MYFUNCTS_H

I had a very similar problem: I solved it by putting the extern "C" around the include in the .cpp file, since the other methods posted here didn't seem to work. 我有一个非常相似的问题:我通过在.cpp文件中的include周围加上了一个extern "C"解决了这个问题,因为此处发布的其他方法似乎无效。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>

extern "C" {
#include "file.h"
#include "other_file.h"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM