- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习使用 android NDK 并尝试创建原生 c++ 类(.h 和 .cpp)。我按照官方教程 ( https://developer.android.com/studio/projects/add-native-code.html ) 来实现这一点。我设法创建了一个简单的 c++ 类并从 java 调用它,没问题。
现在我想创建我自己的 c++ 类(假设是一个 HellowWorld 类),只使用一个什么也不做的构造函数。为此,我右键单击我的 cpp 文件夹,其中包含我已经在工作的 JNI 包装器。
我创建了我的类并创建了一个默认构造函数并从我的 JNI 函数中调用它,但它在编译期间崩溃了:
Error:FAILURE: Build failed with an exception.
出了什么问题:任务“:app:externalNativeBuildDebug”执行失败。
Build command failed. Error while executing 'C:\Users\lucien.moor\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build C:\Users\lucien.moor\Desktop\tmp\MyApplication2\app.externalNativeBuild\cmake\debug\mips64 --target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o [2/2] Linking CXX shared library ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so FAILED: cmd.exe /C "cd . && C:\Users\lucien.moor\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=mips64el-none-linux-android --gcc-toolchain=C:/Users/lucien.moor/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/lucien.moor/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/lucien.moor/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ." CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function
Java_he_1arc_myapplication2_MainActivity_stringFromJNI':
HelloWorld::HelloWorld()' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
C:\Users\lucien.moor\Desktop\tmp\MyApplication2\app\src\main\cpp/native-lib.cpp:10: undefined reference to
尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。
我认为链接 .h 和 .cpp 文件时出现问题。当我尝试内联实现我的构造函数时,它工作正常。它只是找不到 .cpp 实现。
这是我的 JNI native-lib.cpp 文件:
#include <jni.h>
#include <string>
#include "HelloWorld.h"
extern "C"
jstring
Java_he_1arc_myapplication2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
HelloWorld t;
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
这是我的 Helloworld.h:
#ifndef MYAPPLICATION2_HELLOWORLD_H
#define MYAPPLICATION2_HELLOWORLD_H
class HelloWorld {
public:
HelloWorld();
};
#endif //MYAPPLICATION2_HELLOWORLD_H
这是我的 HelloWorld.cpp
#include "HelloWorld.h"
HelloWorld::HelloWorld() { }
当我打开这个文件时,android studio 告诉我“这个文件不是项目的一部分。请将它包含在适当的构建文件(build.gradle、CMakeLists.txt 或 Android.mk 等)中.) 并同步项目。”
那么,如何将这些可爱的 .h 和 .cpp 链接在一起?
最佳答案
我找到了解决方案!
按照消息的建议,我必须将我的文件添加到 CMakeLists.txt 中。不需要头文件,但必须在 CMakeLists.txt 中添加 .cpp 文件
为了让链接器找到实现文件,我不得不添加
src/main/cpp/HelloWorld.cpp in my CMakeLists.txt
这是完整的 CMakeLists 文件:
cmake_minimum_required(VERSION 3.4.1)
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp
src/main/cpp/HelloWorld.cpp)
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
关于android - 如何在 android studio 中创建一个 c++ 类(.h 和 .cpp)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277472/
这个问题在这里已经有了答案: Why can templates only be implemented in the header file? (18 个答案) 关闭 7 年前。 我的 .hpp
我想使用 yaml-cpp 发出一个带引号的字符串,所以它看起来像 时间戳:“2011 年 8 月 10 日 01:37:52” 在输出yaml文件中。我该怎么做?谢谢。 最佳答案 YAML::Emi
我理解了模板的概念以及为什么我们需要在头文件中定义模板成员函数。另一种选择是在 cpp 文件中定义模板函数并显式实例化模板类,如下所示。 模板.h #include using namespace
是否可以发出和读取(解析)二进制数据(图像、文件等)?如下所示: http://yaml.org/type/binary.html我如何在 yaml-cpp 中执行此操作? 最佳答案 截至revisi
我尝试查找此内容并使用头文件等得到混合结果。 基本上我有多个 .cpp 文件,其中包含我为使用二叉树而制作的所有函数,BST , 链表等 我不想复制和粘贴我需要的函数,我只想能够做一个: #inclu
我正在发出一个 YAML 文档,如下所示: YAML::Node doc; // ...populate doc... YAML::Emitter out; out << doc; 在节点层次结构的某
这个问题在这里已经有了答案: Access extern variable in C++ from another file (1 个回答) 关闭 4 年前。 考虑以下场景: MyFile.cpp:
所以我在上基础编程课,我们正在学习如何将文件链接在一起。问题是我遇到了一个似乎没有人能够修复的错误。我已经去过我的教授、学生助理和校园里的编程辅助实验室,但运气不佳。 我还在这里搜索了至少 10 篇与
在下面的代码中,我在使用 parser.GetNextDocument(doc); 解析我的 .yaml 文件时遇到了一些问题。经过大量调试后,我发现这里的(主要)问题是我的 for 循环没有运行,因
我们有以下类(class)考试成绩:完成本类(class)的学生中有 75 人参加了考试。我们想知道学生在考试中的表现如何,并给出了 75 名学生的分数。我们想编写一个程序,按以下方式总结和分析结果:
主要.cpp #include #include #include #include "cootie.h" using namespace std; int main() { cout
试图制作电子鸡程序,但编译器抛出未定义的对“Tamagotchi::age()”错误的引用 理想情况下,这段代码会返回电子鸡的年龄,它应该在开始时由类的构造函数初始化为 0。 我显然在某个地方搞砸了,
我一直在开发一个使用 Microsoft Visual Studio 2010 命令提示符编译原始 .cpp 文件并分析其输出的应用程序。我遇到了很多麻烦,网上似乎没有太多关于这个的资料。这是麻烦的代
我试图从另一个 .cpp 文件调用 c++ 函数。我使用了 .h header 。看看下面我做了什么。 我有一个f.h文件: #ifndef PACKAGENAME_ADD_H #define PAC
我在 CPP 中有一个函数,其原型(prototype)如下: char* complexFunction(char* arg1, ...); 我使用 DLLImport 属性从 C# 导入它。问题是
也许这是一个幼稚的问题 - 但有没有办法构建/安装 yaml-cpp,以便在构建包含 yaml.h 的项目时不需要使用 Boost 库 header ? IE:我正在开发一个使用 yaml-cpp 结
我有一个在 .cpp 函数中声明的静态函数,我不能在 header 中声明它,因为它不应该是可见的。我想在同一项目的另一个 .cpp 中重新使用它。 这有可能吗? 最佳答案 这里有两个问题: 这可能吗
我正在使用 php-cpp 为我的 php 代码创建扩展,当我尝试编译 main.cpp 文件的简单结构时,我得到这个错误。这是编译错误: main.cpp:15:5: error: ‘PHPCPP_
我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 hc_list.h 文件中的 STL 列表包装器模板类。完整代码如下: // hc_list.h file #ifndef HC_LI
您好,我目前正在尝试通过 AMQPCPP 将 RabbitMQ 集成到我的 VisualStudio 项目中。我只能使用 Windows PC,这对安装来说是一件很痛苦的事情。我想我能够使用 CMAK
我是一名优秀的程序员,十分优秀!