QT Subdir- Undefined reference to main - Error

社会主义新天地 提交于 2019-12-23 16:26:13

问题


I have a simple QT subdir project with the structure of

TestProject/
    TestProject.pro
    Subdir1/
            Subdir1.pro
        sources/
            main.cpp
    Subdir2/
        Subdir2.pro
        headers/
            mainwindow.h
        sources/
            mainwindow.cpp

Here are my .pro files TestProject.pro

TEMPLATE = subdirs
SUBDIRS += \
    Subdir1 \
    Subdir2

Subdir1.pro

SOURCES += \
main.cpp

Subdir2.pro

QT += sql

SOURCES += \
    mainwindow.cpp \
HEADERS += \
    mainwindow.h \

When I try to run the application I get the following error:

(.text+0x18):-1: error: undefined reference to `main'

I am using the Qt Creator IDE on Ubuntu 12.04. I have spent all morining trying to figure this out, What must I put in the .pro files in order for Qt to be able to build the project?

Thanks in advance


回答1:


It looks like what you're trying to do is build all your source files into one executable. This is not what the subdirs template is for, it's for creating a single Makefile which builds several different targets (for example two different executables, or an executable and a library).

What you probably want is either to just have a single .pro file with e.g.

SOURCES = Subdir1/mainwindow.cpp Subdir1/main.cpp

Or you can make the subdir pro files into (by convention) .pri files. Then at the top level you would just do:

include(Subdir1/Subdir1.pri)
include(Subdir2/Subdir2.pri)



回答2:


Try to add your Subdir.pro files into your TestProject.pro file explicitly:

OTHER_FILES += \
    Subdir1/Subdir1.pro\
    Subdir2/Subdir2.pro\

so Qt knows there are subprojects.



来源:https://stackoverflow.com/questions/12177241/qt-subdir-undefined-reference-to-main-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!