Qt 创建者:C++:对 Class::Function 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16917677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt creator: c++: undefined reference to Class::Function
提问by hashDefine
I am creating two c++ projects in my Qt creator. The first is an Application project
and the other is unit-test project
. the two projects, separately, work fine. However, when linking the two together I face a little problem.
我正在我的 Qt 创建者中创建两个 C++ 项目。第一个是 an Application project
,另一个是unit-test project
。这两个项目分别运行良好。但是,当将两者联系在一起时,我遇到了一个小问题。
I am including #INCLUDEPATH applicationProjectPath
in .pro file
in the unit-test project
. then #include myClass
from the application project in the main.cpp
of the unit-test project
. then, creating a myObject
from myClass
and calling a function within that object.
我包括#INCLUDEPATH applicationProjectPath
在.pro file
在unit-test project
。然后#include myClass
从应用项目中main.cpp
的unit-test project
. 然后,在该对象中创建一个myObject
frommyClass
并调用一个函数。
when compiling, this error appears:
编译的时候出现这个错误:
undefined reference to `myObject::function'
however, when adding #SOURCES applicationProjectPath/myClass.cpp
to the .pro file of the unit-test project (while keeping the #INCLUDEPATH applicationProjectPath
), everything is working (i.e.: the test units are executed)
但是,当添加#SOURCES applicationProjectPath/myClass.cpp
到单元测试项目的 .pro 文件时(同时保留#INCLUDEPATH applicationProjectPath
),一切正常(即:执行测试单元)
again when removing the #INCLUDEPATH
from the .pro, it again crashes .
再次#INCLUDEPATH
从 .pro 中删除时,它再次崩溃。
I thought if I included #SOURCES
, then I don't need to include the #INCLUDEPATH
. and if I included the #INCLUDEPATH
, I should not include #SOURCES
(at least not with the full path, just the .cpp file and then the compiler should look for both directories, the default one and the added one).
我想如果我包含了#SOURCES
,那么我就不需要包含#INCLUDEPATH
. 如果我包含了#INCLUDEPATH
,我不应该包含#SOURCES
(至少不包含完整路径,只是 .cpp 文件,然后编译器应该查找两个目录,默认目录和添加的目录)。
so, my question is: why is this happening
所以,我的问题是:为什么会发生这种情况
回答by Uflex
Your unit tests will need to compile the classes in your project that you want to unit-test. So you need to add the include in both project (otherwise the test project will not know the classes you are trying to test). And the linker needs to link to the project's code as well, as your tests will use the classes.
您的单元测试将需要编译您要进行单元测试的项目中的类。因此,您需要在两个项目中都添加包含(否则测试项目将不知道您要测试的类)。并且链接器也需要链接到项目的代码,因为您的测试将使用这些类。
One way is to add the classes you want to test in your test project as well and compile them again when you compile your unit test project but this is tedious and not really handy as every time you want to add a class, you need to add it to both .pro files (hint, you can use wildcards in the .pro files, like *.cpp to add all source files in a folder to a project).
一种方法是在测试项目中添加要测试的类,并在编译单元测试项目时再次编译它们,但这很乏味而且不是很方便,因为每次要添加类时,都需要添加它同时添加到 .pro 文件(提示,您可以在 .pro 文件中使用通配符,例如 *.cpp 将文件夹中的所有源文件添加到项目中)。
A better approach in my opinion is to setup the project you want to test as a static library, separating it from the application: you have another project which is an application, containing only the main.cpp
linking to that static library.
在我看来,更好的方法是将要测试的项目设置为静态库,将其与应用程序分开:您有另一个项目,它是一个应用程序,仅包含main.cpp
到该静态库的链接。
Here is a representation of the folder containing the project:
这是包含项目的文件夹的表示:
Project.pro #subdir project
UIProject/ #static lib
UIProject.pro
#all your classes here
MainProject/ #application
MainProject.pro
main.cpp
UITestProject/ #unit tests of UIProject (linking to it)
UITestProject.pro
#all your UI test classes here
Project.pro:
项目.pro:
TEMPLATE = subdirs
SUBDIRS += UIProject
SUBDIRS += MainProject
SUBDIRS += UITestProject
UIProject.pro:
UIProject.pro:
# project type
TEMPLATE = lib
# library type
CONFIG += staticlib
HEADERS += *.h
SOURCES += *.cpp
MainProject.pro:
MainProject.pro:
#[...]
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += ../UIProject/
DEPENDPATH += $${INCLUDEPATH} # force rebuild if the headers change
# link against UILib
_UI_LIB = ../UIProject/
CONFIG(debug, debug|release) {
win32: _UI_LIB = $$join(_UI_LIB,,,debug/UIProject.lib)
} else {
win32: _UI_LIB = $$join(_UI_LIB,,,release/UIProject.lib)
}
LIBS += $${_UI_LIB}
PRE_TARGETDEPS += $${_UI_LIB}
UITestProject.pro:
UITestProject.pro:
#[...]
TEMPLATE = app
HEADERS += *.h
SOURCES += *.cpp
INCLUDEPATH += ../UIProject/
DEPENDPATH += $${INCLUDEPATH} # force rebuild if the headers change
# link against UILib
_UI_LIB = ../UIProject/
CONFIG(debug, debug|release) {
win32: _UI_LIB = $$join(_UI_LIB,,,debug/UIProject.lib)
} else {
win32: _UI_LIB = $$join(_UI_LIB,,,release/UIProject.lib)
}
LIBS += $${_UI_LIB}
PRE_TARGETDEPS += $${_UI_LIB}
You would have to edit that to match your project but the main things are here. It should work as I copied it from one of my project, providing I didn't add any errors.
您必须编辑它以匹配您的项目,但主要内容在这里。它应该可以在我从我的一个项目中复制它时工作,前提是我没有添加任何错误。
回答by Lol4t0
you should include your cpp file to the project to tell build system (qmake) to compile the given file.
您应该将您的 cpp 文件包含到项目中,以告诉构建系统 (qmake) 编译给定的文件。
When you add
myClass.cpp
to test project, it gets compiled, otherwise does not.
当您添加
myClass.cpp
到测试项目时,它会被编译,否则不会。
So, when you will not add cpp file to the project, you'll get linkererror.
因此,当您不将 cpp 文件添加到项目时,您将收到链接器错误。
Includes are just handled separately
包含只是单独处理
When you add new path to INCLUDEPATH
, compiler will add that pass to headers search list
当您向 中添加新路径时INCLUDEPATH
,编译器会将该传递添加到标头搜索列表中
When you add
applicationProjectPath
to the include path, compiler will search headers there. It is important, as allows compiler to findmyClass.h
当您添加
applicationProjectPath
到包含路径时,编译器将在那里搜索头文件。这很重要,因为允许编译器找到myClass.h
If you will not add path to headers search list, compiler will not be able to compile your main.cpp
and you'll get compilationerror.
如果您不向标头搜索列表添加路径,编译器将无法编译您的,main.cpp
并且您将收到编译错误。
You, however, can just specify (relative) path to your myClass.h
in your test-project's main:
但是,您可以myClass.h
在测试项目的主要内容中指定(相对)路径:
#include "../applicationProjectPath/myClass.h"
Summing up
加起来
You need add your source files to test project andadd your headers path to test project's include search list.
您需要将源文件添加到测试项目,并将标头路径添加到测试项目的包含搜索列表。
Further reading
进一步阅读
What is an undefined reference/unresolved external symbol error and how do I fix it?