C:\Users\Eng.Beshoy\Documents\Admob\widget.cpp:12:...

Tạo vào: 26 tháng 4, 2026

Trả lời bằng GPT-5.5 Thinking bởi Chat01

Câu hỏi

C:\Users\Eng.Beshoy\Documents\Admob\widget.cpp:12: error: 'QNativeInterface' file not found
C:\Users\Eng.Beshoy\Documents\Admob\widget.cpp:12:10: fatal error: 'QNativeInterface' file not found
12 | #include <QNativeInterface>
| ^~~~~~~~~~~~~~~~~~

here is my cmake

cmake_minimum_required(VERSION 3.16)

project(Admob VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

set(PROJECT_SOURCES
main.cpp
widget.cpp
widget.h
widget.ui
)

if(QTVERSIONMAJORGREATEREQUAL6)qtaddexecutable(AdmobMANUALFINALIZATION{QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(Admob MANUAL_FINALIZATION {PROJECT_SOURCES}
AdMobBridge.java
)

set_target_properties(Admob PROPERTIES
QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")

Define target properties for Android with Qt 6 as:

set_property(TARGET Admob APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR

${CMAKE_CURRENT_SOURCE_DIR}/android)

For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation

else()
if(ANDROID)
add_library(Admob SHARED
${PROJECT_SOURCES}
)

Define properties for Android with Qt 5 after find_package() calls as:

set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")

text
else() add_executable(Admob ${PROJECT_SOURCES} ) endif()

endif()

if(ANDROID)
set_property(TARGET Admob PROPERTY
QT_ANDROID_PACKAGE_SOURCE_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/android"
)
endif()

target_link_libraries(Admob PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.

If you are developing for iOS or macOS you should consider setting an

explicit, fixed bundle identifier manually though.

if(QTVERSIONVERSIONLESS6.1.0)set(BUNDLEIDOPTIONMACOSXBUNDLEGUIIDENTIFIERcom.example.Admob)endif()settargetproperties(AdmobPROPERTIES{QT_VERSION} VERSION_LESS 6.1.0) set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.Admob) endif() set_target_properties(Admob PROPERTIES {BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION PROJECTVERSIONMACOSXBUNDLESHORTVERSIONSTRING{PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING {PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS Admob
BUNDLE DESTINATION .
LIBRARY DESTINATION CMAKEINSTALLLIBDIRRUNTIMEDESTINATION{CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION {CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(Admob)
endif()

Suy nghĩ

undefined

Suy nghĩ

undefined

Suy nghĩ

undefined

Suy nghĩ

Clarifying Qt version setup for AdMob Android

In your CMake, you need to allow Qt5, but QNativeInterface only works with Qt6, with QAndroidApplication starting from version 6.2. If you're building with a Qt5 kit or older Qt6, you'll likely encounter issues. I suggest updating to Qt6.2+ and adjusting CMake. Make sure that if you target Android, guard against including Android/Native stuff for desktop builds. If you're compiling with a desktop kit, the path error likely won't affect Android targets.

Thought for 34s

Trả lời

The error is not from your CMake syntax. It is because your project is being built with Qt 5 or with an old Qt 6 kit. QNativeInterface is a Qt 6 header, and the Android API you probably want, QNativeInterface::QAndroidApplication, is available since Qt 6.2. Qt documents QNativeInterface as #include <QNativeInterface> since Qt 6.0, and QAndroidApplication::context() since Qt 6.2. (Qt Documentation)

Your CMake currently allows both Qt 6 and Qt 5:

cmake
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

So if Qt Creator is using a Qt 5 kit, CMake happily configures Qt 5, then this fails:

cpp
#include <QNativeInterface>

Recommended fix: require Qt 6.2+

Change the top Qt part of your CMake to this:

cmake
find_package(Qt6 6.2 REQUIRED COMPONENTS Core Widgets)

and link Core explicitly:

cmake
target_link_libraries(Admob PRIVATE Qt6::Core Qt6::Widgets)

So your cleaned-up CMake should look like this:

cmake
cmake_minimum_required(VERSION 3.16) project(Admob VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 REQUIRED COMPONENTS Core Widgets) set(PROJECT_SOURCES main.cpp widget.cpp widget.h widget.ui ) qt_add_executable(Admob MANUAL_FINALIZATION ${PROJECT_SOURCES} ) target_link_libraries(Admob PRIVATE Qt6::Core Qt6::Widgets) if(ANDROID) set_property(TARGET Admob PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android" ) endif() set_target_properties(Admob PROPERTIES MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS Admob BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) qt_finalize_executable(Admob)

Also guard the Android-only code

In widget.cpp, do not include Android/Qt-native-interface headers unconditionally. Use this:

cpp
#include <QtGlobal> #if defined(Q_OS_ANDROID) #include <QJniObject> #include <QNativeInterface> #endif

Then any Android call should also be guarded:

cpp
#if defined(Q_OS_ANDROID) QJniObject context = QNativeInterface::QAndroidApplication::context(); // Example: QJniObject::callStaticMethod<void>( "com/your/package/AdMobBridge", "initialize", "(Landroid/content/Context;)V", context.object<jobject>() ); #endif

QJniObject is the Qt 6 JNI wrapper used for calling Java from C++, and Qt’s Android examples use it together with QNativeInterface::QAndroidApplication::context(). (Qt Documentation)

Check your kit in Qt Creator

In Qt Creator, make sure you are building with something like:

text
Qt 6.2+ for Android

not:

text
Desktop Qt 5.x MinGW Desktop Qt 6.x MinGW

or:

text
Android Qt 5.x

For this specific QNativeInterface::QAndroidApplication approach, use Qt 6.2 or newer for Android.

One more note about AdMobBridge.java

This line is probably not useful:

cmake
AdMobBridge.java

Java files for Android should usually live under your Android package source directory, for example:

text
android/src/com/your/package/AdMobBridge.java

Your CMake already points Qt to that Android folder with:

cmake
QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android"

So remove AdMobBridge.java from qt_add_executable() and put it in the correct android/src/... Java package path.

Chia sẻ Q&A này