Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将标准库导入本机android项目中的头文件 我试图把一个PC C++项目编译成Android。 我编译了这个模板示例,它在我的设备上也运行得很好_Android_C++_Cmake_Android Ndk - Fatal编程技术网

无法将标准库导入本机android项目中的头文件 我试图把一个PC C++项目编译成Android。 我编译了这个模板示例,它在我的设备上也运行得很好

无法将标准库导入本机android项目中的头文件 我试图把一个PC C++项目编译成Android。 我编译了这个模板示例,它在我的设备上也运行得很好,android,c++,cmake,android-ndk,Android,C++,Cmake,Android Ndk,起初,我只创建了一个简单的头文件,但无法导入简单的标准库,例如: #包括 奇怪的是,如果我将其更改为cpp文件,所有内容都可以导入 这就是我所做的: CMakeList.txt # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the mi

起初,我只创建了一个简单的头文件,但无法导入简单的标准库,例如:
#包括

奇怪的是,如果我将其更改为cpp文件,所有内容都可以导入

这就是我所做的:

CMakeList.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Werror")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# now build app's shared lib

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        native-lib.cpp
        BoolPair.h
        )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
应用程序build.gradle

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
                cppFlags += "-std=c++11"

            }
        }
    }

您尚未指定要在CMake文件中支持哪种语言。常见的CMake习惯用法是在文件顶部指定调用,可以选择指定目标语言:

cmake_minimum_required(VERSION 3.4.1)
project(MyProjectName CXX)

当未指定语言时,CMake默认启用C语言和C++语言。 尚不清楚您是如何将

.h
文件仅添加到目标
本机库的。这通常会导致CMake错误:

CMake can not determine linker language for target
无论如何,在您的例子中,CMake选择C编译器似乎是因为没有明确指定该语言。对于C编译器,不支持包含


另一方面,通过包含<代码> .CPP < /C>文件和页眉,CMake现在根据文件扩展名确定这是C++源代码。在这种情况下,使用C++编译器,而>/COD>被接受。

< P>您没有指定您要在CFALE文件中支持哪种语言。常见的CMake习惯用法是在文件顶部指定调用,可以选择指定目标语言:

cmake_minimum_required(VERSION 3.4.1)
project(MyProjectName CXX)

当未指定语言时,CMake默认启用C语言和C++语言。 尚不清楚您是如何将

.h
文件仅添加到目标
本机库的。这通常会导致CMake错误:

CMake can not determine linker language for target
无论如何,在您的例子中,CMake选择C编译器似乎是因为没有明确指定该语言。对于C编译器,不支持包含


另一方面,通过包含<代码> .CPP < /C>文件和页眉,CMake现在根据文件扩展名确定这是C++源代码。在这种情况下,使用C++编译器,而<代码> <代码>被接受。

“因此,当您只包含<代码> .h /code >文件时,CMake确定这是C代码”- CMake不区分头文件和其他非源文件。如果没有为库指定任何源文件,则CMake将发出一个错误,说明没有检测到链接的语言。@Tsyvarev您是正确的。我只是在我的机器上测试了这个,所以不确定OP是如何产生这种行为的。当C++实际上已经被指定时,我仍然相信C编译器正在被使用。我将更新答案的措辞。“因此,当您只包含
.h
文件时,CMake会确定这是C代码”-CMake不会区分头文件和其他非源文件。如果没有为库指定任何源文件,则CMake将发出一个错误,说明没有检测到链接的语言。@Tsyvarev您是正确的。我只是在我的机器上测试了这个,所以不确定OP是如何产生这种行为的。当C++实际上已经被指定时,我仍然相信C编译器正在被使用。我将更新答案的措辞。“我只创建了一个简单的头文件,但我无法导入简单的标准库,例如:
#include
…如果我将其更改为cpp文件,则所有内容都可以导入。”-那么您显示的代码是否有效?如果它有效,你为什么要展示它?你想从我们这里得到什么?如果它不起作用,请显示您收到的确切错误消息。“我只创建了一个简单的头文件,但我无法导入简单的标准库,例如:
\include
…如果我将其更改为cpp文件,所有内容都可以导入。”-那么您显示的代码是否起作用?如果它有效,你为什么要展示它?你想从我们这里得到什么?如果不起作用,请显示您收到的确切错误消息。