1、使用CMake进行跨平台
cmake(6):适配多种操作系统编译构建以及if命令_翔底的博客-CSDN博客_cmake if(unix)
如何写出优雅的跨平台的C/C++代码_芥末的无奈的博客-CSDN博客
2、标志位
// a.cpp #include "a.h" #ifdef _WIN32_ void print_something() { printf("_WIN32_"); } #ifdef _LINUX_ void print_something() { printf("_LINUX_"); } #ifdef _IOS_ void print_something() { printf("_IOS_"); } #ifdef _ANDROID_ void print_something() { printf("_ANDROID_"); } #endif
检测架构
/* Identify known platforms by name. */ #if defined(__linux) || defined(__linux__) || defined(linux) # define PLATFORM_ID "Linux" #elif defined(__CYGWIN__) # define PLATFORM_ID "Cygwin" #elif defined(__MINGW32__) # define PLATFORM_ID "MinGW" #elif defined(__APPLE__) # define PLATFORM_ID "Darwin" #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) # define PLATFORM_ID "Windows"
检测平台
#if defined(_WIN32) && defined(_MSC_VER) # if defined(_M_IA64) # define ARCHITECTURE_ID "IA64" # elif defined(_M_X64) || defined(_M_AMD64) # define ARCHITECTURE_ID "x64" # elif defined(_M_IX86) # define ARCHITECTURE_ID "X86" # elif defined(_M_ARM) # if _M_ARM == 4 # define ARCHITECTURE_ID "ARMV4I" # elif _M_ARM == 5 # define ARCHITECTURE_ID "ARMV5I" # else
跨平台:GN实践详解(ninja, 编译, windows/mac/android实战)
linux 静态库跨平台编译
GCC编译静态库的-fPIC选项_太上绝情的博客-CSDN博客_-fpic 静态库
cmake时添加-fPIC编译选项_阿飞0308的博客-CSDN博客_cmake fpic
# 1 add_compile_options(-fPIC) #2 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpic") #3 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
CMake 在Linux上使用动态库和静态库_hp_cpp的博客-CSDN博客_cmake使用动态库
target_link_libraries(myProject libhello.so) #这些库名写法都可以。 target_link_libraries(myProject hello) target_link_libraries(myProject -lhello)
https://www.jianshu.com/p/37fbe3dd202b
# 以导入外部库的方式(不会重新编译)重新构建一个静态库libMath add_library(libMath STATIC IMPORTED) # 设置要导入的外部静态库的路径 set_target_properties(libMath PROPERTIES IMPORTED_LOCATION ${ROOT_DIR}/3rdlib/libmath.a)
Android cmake
CMake | Android NDK | Android Developers
GitHub - android/ndk-samples: Android NDK samples with Android Studio
C++ 库支持 | Android NDK | Android Developers
向您的项目添加 C 和 C++ 代码 | Android 开发者 | Android Developers
ExternalNativeBuildOptions | Android Developers
关于abiFilters的使用_只是备忘的博客-CSDN博客_abifilters
Android NDK学习(六): so文件兼容之abiFilters的使用 - JavaShuo
AndroidStudio创建jniLibs文件夹添加jar包\so库_Rooney8的博客-CSDN博客_android studio 添加jnilibs
Android Studio gardle 配置 ndk 指定 ABI: abiFilters 详解_阿飞__的博客-CSDN博客_abifilters
android { compileSdkVersion 30 buildToolsVersion "30.0.2" ndkVersion '22.1.7171670' defaultConfig { applicationId "com.lj.ainative" minSdkVersion 16 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "" } } ndk { abiFilters "arm64-v8a" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" version '3.18.1' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } sourceSets { main { jniLibs.srcDirs = ['libs'] } } }
示例:
#include <string.h> #include <jni.h> JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { #define ABI "x86_64" return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI "."); }
cmake_minimum_required(VERSION 3.4.1) project(hello-jni C CXX) add_library(hello-jni SHARED hello-jni.c) # Include libraries needed for hello-jni lib target_link_libraries(hello-jni android log)
编译:linux下
#!/bin/bash export NDK=/home/xx/android/ndk cmake ../../../ \ -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ -DCMAKE_BUILD_TYPE=Release \ -DANDROID_ABI="arm64-v8a" \ -DANDROID_STL=c++_static \ -DANDROID_NATIVE_API_LEVEL=android-21 \ -DNATIVE_LIBRARY_OUTPUT=. -DNATIVE_INCLUDE_OUTPUT=. make -j4
编译:windows下
@echo off set ANDROID_NDK=D:/Android/Sdk/ndk/21.3.6528147 cmake .. ^ -G "Unix Makefiles" ^ -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%/build/cmake/android.toolchain.cmake ^ -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK%/prebuilt/windows-x86_64/bin/make.exe ^ -DCMAKE_BUILD_TYPE=Release ^ -DANDROID_ABI="arm64-v8a" ^ -DANDROID_STL=c++_static ^ -DANDROID_NATIVE_API_LEVEL=android-21 ^ -DNATIVE_LIBRARY_OUTPUT=. -DNATIVE_INCLUDE_OUTPUT=. echo: echo begin to build============================================ echo. cmake --build . -- -j5 REM %ANDROID_NDK%/prebuilt/windows-x86_64/bin/make -j4 pause
Windows下使用CLion进行NDK编程(CMake) - 灰信网(软件开发博客聚合)
cmake(1) — CMake 3.24.2 Documentation
Android NDK开发扫盲及最新CMake的编译使用 - 知乎 借助Android Gradle
浏览器上的IDE:code-server安装——服务器版的vscode - 知乎
GNU与c++STL的区别与联系_龙行天下01的博客-CSDN博客_gnustl stdc++区别
Qt CMake
修改 CMakeLists.txt,添加搜索路径:
# 添加 begin set(CMAKE_PREFIX_PATH "D:/Qt6/6.2.2/msvc2019_64") list(APPEND CMAKE_PREFIX_PATH /home/Qt/5.15.2/gcc_64) message("CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}") # 添加 end find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
Failed to find "GL/gl.h" in "/usr/include/libdrm"_aiyun5666的博客-CSDN博客