一個好的工具環(huán)境或許能更容易入門拟糕,我選擇的是cloin判呕,支持語法提示倦踢,高亮。
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" ABSOLUTE) #原封不動取目錄或包含路徑的文件 如果不填寫路徑會自動 添加上自己項目路徑,也就是CMAKE_CURRENT_LIST_DIR和它拼接
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" NAME) #取文件名,不包含路徑
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" EXT) #取擴(kuò)展名
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" REALPATH) #和ABSOLUTE 一樣侠草,
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" PATH) #只保留路徑
完整代碼
# cmake_minimum_required(VERSION <specify CMake version here>)
message(${BUILD_TYPE})
if (BUILD_TYPE STREQUAL arm)#判斷字符串是否等于arm
# project(MyApplication)
cmake_minimum_required(VERSION 3.4.1)
message("is arm set version 3.12")
set(CMAKE_C_FLAGS "-std=gnu99")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
message(${CMAKE_C_FLAGS})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie") #設(shè)置變量 CMAKE_EXE_LINKER_FLAGS為右邊的內(nèi)容辱挥。
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
# 配置使用 NDK Standalone Toolchain 編譯
set(NDK_STANDALONE_TOOLCHAIN E:/cross-c/ndk_toolchain)
set(CMAKE_SYSTEM_NAME AndroidARM)
set(CMAKE_SYSTEM_VERSION 3)
set(CMAKE_C_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-g++)
set(CMAKE_FIND_ROOT_PATH ${NDK_STANDALONE_TOOLCHAIN})
# 使用 NDK 提供的頭文件
#add_definitions("--sysroot=E:/android_source/android-4.1.1_r1/android-4.1.1_r1/prebuilts/ndk/android-ndk-r7/platforms/android-9/arch-arm")
add_definitions("--sysroot=${NDK_STANDALONE_TOOLCHAIN}/sysroot")
else ()
set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
project(MyApplication)
set(CMAKE_CXX_STANDARD 11)
message("window set var")
message(WARNING "xxxxx")
message(${CMAKE_CURRENT_LIST_DIR}) #/cygdrive/d/luozheng/cproject/MyApplication 當(dāng)前目錄
endif ()
set(NAME qssq666)
message(${NAME})
if (NAME)
message("has set var NAME=${NAME}")
list(APPEND LIST_VAR "--sysroot ${ANDROID_SYSTEM_LIBRARY_PATH} XXX XXX XXX XXX")
unset(NAME) #只能清空變量
if (NAME)
message("name deleted")
else ()
message("name delete fail var is ${NAME}")
endif ()
message(${LIST_VAR})
else ()
message("not set var NAME")
endif ()
message("ndk path${ANDROID_NDK} ANDROID_NDK_EXPECTED_PATH ${ANDROID_NDK_EXPECTED_PATH}")
aux_source_directory(src SOURCE_FILES) #添加一個目錄
add_executable(good src/hello.c)
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" ABSOLUTE) #原封不動取目錄或包含路徑的文件 如果不填寫路徑會自動 添加上自己項目路徑,也就是CMAKE_CURRENT_LIST_DIR和它拼接
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" NAME) #取文件名,不包含路徑
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" EXT) #取擴(kuò)展名
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" REALPATH) #和ABSOLUTE 一樣,
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "d:/ahello.c" PATH) #只保留路徑
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) #/cygdrive/d/luo zheng 向上取兩級
#get_filename_component(ANDROID_NDK_EXPECTED_PATH "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE)
#add_executable(good src/test12.c)
#add_executable(hello src/hello1.c)
#add_executable(hello main.cpp test1.cpp test1.h)
生成警告信息
更多參考
https://cmake.org/cmake/help/v3.10/#
set toolchain=%ANDROID_HOME%/ndk-bundle/build/cmake/android.toolchain.cmake
::set toolchain=%ANDROID_HOME%\cmake\3.6.4111459\android.toolchain.cmake
set android_ndk=%ANDROID_HOME%/ndk-bundle
set build_type=Release
set gernerator="Ninja"
if not exist %1 md %1
cd %1
cmake ../.. -DCMAKE_TOOLCHAIN_FILE=%toolchain% -DANDROID_NDK=%android_ndk% -DCMAKE_BUILD_TYPE=%build_type% -DANDROID_ABI="%1" -DCMAKE_GENERATOR=%gernerator%
ninja
其中-DCMAKE_BUILD_TYPE=
是表明創(chuàng)建一個變量CMAKE_BUILD_TYPE
獲取當(dāng)前目錄
message(${CMAKE_CURRENT_LIST_DIR})