Issue
I'm currently attempted to use Qt project in the way that cmake compile project. I worked with Qt creator 4.11.1. I make new exercise in which the way of compilation is selected with cmake. It's most simple test. after all done,I used the cmake gui tool convert cmakelists to vs 2017 sln. the content of cmakelists was written in following sections:
cmake_minimum_required(VERSION 3.5)
project(Test_C LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
find_package(Qt5 COMPONENTS Widgets REQUIRED)
if(ANDROID)
add_library(Test_C SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(Test_C
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
endif()
target_link_libraries(Test_C PRIVATE Qt5::Widgets)
Then, Opening the sln by vs 2017, everything was well in first compile.However, it will get error message when I modified the mainwindow.h header file. It seems contain the marco Q_OBJECT, the error message is:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: virtual void * __cdecl MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UEAAPEAXPEBD@Z) Test_C D:\Alex\None-Working\Exercise\Qt\Test_C\Test_CB\Build\mainwindow.obj 1
Why is this happening (what did I do wrong), and how can I fix it (how do I do it right)?
Update: There is no any specifical modification to mainwindow.h. otherwise, Adding the header file: before modification:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
after modification:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qstring.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
it may be not compiled if any modification occurred I guess. I do not insure whether moc compiler was worked in that time
Solution
In the span of past days, It's definitely answer to this problem caused by an environment where the source code was protected in technique of encryption, which established by my company.Hence, it's a peculiar case as a rule. Thanks john for your reply. This answer should be closed
Answered By - Savner_Dig Answer Checked By - Marie Seifert (WPSolving Admin)