Trying to use CMake when cross compiling c/c++/cuda program - cmake

I am trying out to use CMake to cross-compile a program, but it failed with this message:
/bin/sh: 1: Syntax error: ";;" unexpected
The program compiles perfectly with a raw Makefile
Here is my CMake file:
cmake_minimum_required(VERSION 3.9)
project(my_target LANGUAGES CUDA CXX C)
include(CheckLanguage)
check_language(CUDA)
# STD
if (NOT DEFINED CMAKE_CUDA_STANDARD)
set(CMAKE_CUDA_STANDARD 11)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
endif()
find_package(Poco REQUIRED Foundation Util)
# Flags
set (
CMAKE_C_STANDARD 11
)
set(
CMAKE_CXX_STANDARD 11
)
set(
CMAKE_C_FLAGS
${CMAKE_C_FLAGS}
-c -Wall -fopenmp
)
set(
CMAKE_CXX_FLAGS
${CMAKE_CXX_FLAGS}
-c -Wall -fopenmp
)
set(
CMAKE_CUDA_FLAGS
${CMAKE_NVCC_FLAGS}
--ccbin $(CMAKE_CXX_COMPILER) -O2 -gencode -std=c++11 -dc -Werror deprecated-declarations
)
# Compile Objects
file(GLOB_RECURSE src_c src/*.c )
file(GLOB_RECURSE src_cpp src/*.cpp)
file(GLOB_RECURSE src_cu src/*.cu )
file(GLOB_RECURSE src_h src/*.h )
add_library(cobjs STATIC ${src_c} ${src_h} )
add_library(cppobjs STATIC ${src_cpp} ${src_h})
add_library(cuobjs STATIC ${src_cu} ${src_h} )
target_compile_options(cobjs PUBLIC ${CMAKE_C_FLAGS} )
target_compile_options(cppobjs PUBLIC ${CMAKE_CXX_FLAGS})
target_compile_options(cuobjs PUBLIC ${CMAKE_NVCC_FLAGS})
set_target_properties( cuobjs
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
add_executable(my_target
${cobjs} ${cppobjs} ${cuobjs}
${src_h}
)
# Include Dirs
target_include_directories(cobjs PUBLIC
include src
)
target_include_directories(cppobjs PUBLIC
include src
)
target_include_directories(cuobjs PUBLIC
include src
)
target_include_directories(my_target PUBLIC
include src
)
link_directories(
${POCO_HOME}
${METIS_HOME}
)
target_link_libraries(my_target PUBLIC
cobjs
cppobjs
cuobjs
-lPocoFoundation
-lPocoUtil
-lpthread
-lgomp
-lmetis
)
And the working Makefile:
#===============================================================================
CUDA_PATH := /usr/local/cuda
METIS_PATH := ${METIS_HOME}
POCO_PATH := ${POCO_HOME}
# D-debug R-release
COMPILEMD := R
EXE := coupled.exe
CC := gcc
CXX := g++
#===============================================================================
DIR_BIN := bin
DIR_SRC := src
CCFLAGS := -Iinclude -I$(DIR_SRC) -std=c11 -c -Wall -fopenmp
CXXFLAGS := -I$(CUDA_PATH)/include -I$(METIS_PATH)/include -I$(POCO_PATH)/include -Iinclude -I$(DIR_SRC) -std=c++11 -c -Wall -fopenmp
NVCCFLAGS := -I$(POCO_PATH)/include -I$(DIR_SRC) -std=c++11 -dc -Werror deprecated-declarations
LDFLAGS := -L$(METIS_PATH)/lib -lmetis -L$(POCO_PATH)/lib -lPocoUtil -lPocoFoundation -Llib -ltecio -lpthread -lgomp
BASENM := $(basename $(EXE))
SUFFIX := $(suffix $(EXE))
SRC_C := $(shell find $(DIR_SRC) -name *.c)
SRC_CPP := $(shell find $(DIR_SRC) -name *.cpp)
SRC_CU := $(shell find $(DIR_SRC) -name *.cu)
OBJ_C := $(SRC_C:%.c=%.o)
OBJ_CPP := $(SRC_CPP:%.cpp=%.o)
OBJ_CU := $(SRC_CU:%.cu=%.o)
ifeq ($(COMPILEMD), D)
CCFLAGS += -D_DEBUG -g
CXXFLAGS += -D_DEBUG -g
NVCCFLAGS += -D_DEBUG -g -G
else ifeq ($(COMPILEMD), R)
CCFLAGS += -DNDEBUG -O2
CXXFLAGS += -DNDEBUG -O2
NVCCFLAGS += -DNDEBUG -O2
else
$(error error COMPILEMD($(COMPILEMD)))
endif
CCFLAGS += -DKS_FP_DOUBLE -march=native
CXXFLAGS += -DKS_FP_DOUBLE -march=native
NVCCFLAGS += -DKS_FP_DOUBLE
BASENM := $(BASENM)DP$(COMPILEMD)
ifeq ($(SRC_CU), )
LD := $(CXX)
else
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(CXX)
DEVICE_SM := 50 52 60 61 #70 75
$(foreach SM,$(DEVICE_SM),$(eval NVCCFLAGS += -gencode arch=compute_$(SM),code=sm_$(SM)))
$(foreach SM,$(DEVICE_SM),$(eval LDFLAGS += -gencode arch=compute_$(SM),code=sm_$(SM)))
LD := $(NVCC)
endif
OBJ := $(OBJ_C) $(OBJ_CPP) $(OBJ_CU)
EXE := $(DIR_BIN)/$(BASENM)$(SUFFIX)
#===============================================================================
all: $(EXE)
.PHONY: all clean cleanall
$(EXE): $(OBJ)
#mkdir -p $(DIR_BIN)
$(LD) $^ $(LDFLAGS) -o $#
$(OBJ_C): %.o: %.c
$(CC) $< $(CCFLAGS) -o $#
$(OBJ_CPP): %.o: %.cpp
$(CXX) $< $(CXXFLAGS) -o $#
$(OBJ_CU): %.o: %.cu
$(NVCC) $< $(NVCCFLAGS) -o $#
clean:
-rm -rf $(OBJ) $(EXE)
cleanall:
-rm -rf $(OBJ) $(DIR_BIN)/*log $(DIR_BIN)/*.exe $(DIR_BIN)/*.dat
I am sorry that I could not provide the project's source code.
I am very new to CMake, and any help will be appreciated!
EDIT
I tried the solutions given by #squareskittles which seems to solve the /bin/sh: 1: Syntax error: ";;" unexpected problem. But I got a new error which says:
c++: fatal error: no input files
Yet in the corresponding build.make file the error line writes:
/usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -o <path-to-my-cpp>/my.cpp.o -c <path-to-my-cpp>/my.cpp
EDIT2
Thanks to #squareskittles's answer, I finally get to understand how CMake works. There are actually more than one incorrect parts in the CMakeLists.txt file:
CMake adds automatically -c flags to C and C++ compilers so there is no need to add it manually.
It is preferred to use add_compile_options() to add compilation flags than set(CMAKE_<LANG>_FLAGS ...).
link_directories should always go before add_library() and add_executable(). A modern approach instead is to use find_library(), see here.
I finally gave up to manually write multiple stage compilation as I did in my Makefile, and instead added all source files in add_executable(). CMake seems to have handled the different cases perfectly.
My final working version:
cmake_minimum_required(VERSION 3.9)
project(my_target LANGUAGES CUDA CXX C)
# STD
if (NOT DEFINED CMAKE_CUDA_STANDARD)
set(CMAKE_CUDA_STANDARD 11)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
endif()
find_package(Poco REQUIRED Foundation Util)
# Flags
set (
CMAKE_C_STANDARD 11
)
set(
CMAKE_CXX_STANDARD 11
)
# Compile Objects
file(GLOB_RECURSE src_c src/*.c )
file(GLOB_RECURSE src_cpp src/*.cpp)
file(GLOB_RECURSE src_cu src/*.cu )
file(GLOB_RECURSE src_h src/*.h )
# message(STATUS "src_c = ${src_c}" )
# message(STATUS "src_cpp = ${src_cpp}")
# message(STATUS "src_cu = ${src_cu}" )
# message(STATUS "src_h = ${src_h}" )
add_definitions(-DKS_FP_DOUBLE)
link_directories(
${POCO_HOME}
${METIS_HOME}
)
add_executable(my_target
${src_c}
${src_cpp}
${src_cu}
${src_h}
)
# Include Dirs
target_include_directories(my_target PUBLIC
include src
)
set_target_properties(my_target
PROPERTIES CUDA_SEPARABLE_COMPILATION ON
)
target_link_libraries(my_target PUBLIC
# cobjs
# cppobjs
# cuobjs
-lPocoFoundation
-lPocoUtil
-lpthread
-lgomp
-lmetis
)

When you set the CMake compiler flag variables (or any other CMake variable), you don't need to insert semicolons ; to separate the arguments/flags. CMake will recognize newlines and spaces in the set() command and handle this for you. From the docs:
Multiple arguments will be joined as a semicolon-separated list to form the actual variable value to be set.
Try this:
set(
CMAKE_C_FLAGS
${CMAKE_C_FLAGS}
-c -Wall -fopenmp
)
set(
CMAKE_CXX_FLAGS
${CMAKE_CXX_FLAGS}
-c -Wall -fopenmp
)
set(
CMAKE_CUDA_FLAGS
${CMAKE_NVCC_FLAGS}
--ccbin $(CMAKE_CXX_COMPILER) -O2 -gencode -std=c++11 -dc -Werror deprecated-declarations
)
There is one more issue with the CMake file you posted. The add_executable() command only takes source files as arguments. You cannot use previously-defined targets as arguments. Try modifying your call to add_executable(); you already compile the C, C++, and Cuda sources into static libraries with your calls to add_library(), so maybe there is simply a main.cpp (defining your call to main()) you can include in this call:
add_executable(my_target
main.cpp
${src_h}
)

Related

Why does CMake repeat link libraries across link flags?

I'm using a third party project that provides dependencies to link against via a pkg-config file, which I pass to my target via target_link_libraries. The final result looks something like this example:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(main)
add_executable(main main.c)
target_link_libraries(
main
PRIVATE -lfoo
-Wl,--whole-archive
-lbar
-Wl,--no-whole-archive
-lfoo
-Wl,-Bdynamic
-lbaz)
Where main.c is a trivial C source file:
$ cat main.c
int main() { return 0; }
When I run CMake's configure step and inspect the generated compile commands, I see some libraries duplicated across link flags:
$ cmake -S . -B build -GNinja
$ ninja -C build -t commands
/usr/bin/cc -MD -MT CMakeFiles/main.dir/main.c.o -MF CMakeFiles/main.dir/main.c.o.d -o CMakeFiles/main.dir/main.c.o -c /home/ja/toy/main.c
: && /usr/bin/cc CMakeFiles/main.dir/main.c.o -o main -lfoo -Wl,--whole-archive -lbar -Wl,--no-whole-archive -lfoo -Wl,-Bdynamic -lbaz -lbar -lbaz && :
Clearly, -lbar was repeated across --no-whole-archive and -Bdynamic. Is this equivalent to the original target_link_libraries call? Why does CMake do this?
Additionally, I tried experimenting without the link flags, and noticed that CMake transforms -lfoo -lbar -lfoo -lbaz into -lfoo -lbar -lfoo -lbaz -lbar -lbaz. I suspect it might have something to do with the behavior described at LINK_INTERFACE_MULTIPLICITY but I do not understand the rationale behind doing this across link flags.
Edit: I'm using CMake version 3.23.0.

cmake extract substring from variable

I have variable CC_OPTIONS has value set like below
-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11
I wanted to extract -mcpu=abc1 from CC_OPTIONS
Tried below approach, but getting more than what i wanted.
string(REGEX REPLACE ".*mcpu=(.*)\ .*" "\\1" CPU_TYPE "${CC_OPTIONS}")
any suggestions?
If you use if(MATCHES) you can get character groups of the match using CMAKE_MATCH_<n>
set(MY_VAR "-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11")
if (MY_VAR MATCHES "^([^ ]+ +)*(-mcpu=[^ ]+)( +[^ ]+)*$")
message(STATUS "Found match: ${CMAKE_MATCH_2}")
else()
message(FATAL_ERROR "mismatch")
endif()
Like that:
cmake_minimum_required (VERSION 2.8.11)
project (HELLO)
set(CC_OPTIONS "-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11")
message(${CC_OPTIONS})
string(REGEX MATCH "\\-mcpu=[^ $]+" CPU_TYPE ${CC_OPTIONS})
message(${CPU_TYPE})
Example:
$ cmake .
-arch arm64 -mcpu=abc1 -c --debug -O2 -static -fstack-protector -ffreestanding -nostartfiles -std=c11
-mcpu=abc1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ja/cmake

MinGW+cmake on Windows 10 produce error: '__MINGW_EXTENSION' does not name a type

I have spent long time developing C/C++ under Linux, now I'm trying to port/compile my project under windows where I have no experinece. I want to keep the workflow as close to Linux as possible so I go with g++/MinGW and cmake.
When I do standard approach
cd Build
cmake .. -G "MinGW Makefiles"
make
I got tons of following errors:
PS D:\git\SimpleSimulationEngine\cpp\Build> make VERBOSE=1
"C:\Program Files\CMake\bin\cmake.exe" -SD:\git\SimpleSimulationEngine\cpp -BD:\git\SimpleSimulationEngine\cpp\Build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\SimpleSimulationEngine\cpp\Build\CMakeFiles D:\git\SimpleSimulationEngine\cpp\Build\\CMakeFiles\progress.marks
C:/MinGW/bin/make -f CMakeFiles\Makefile2 all
make[1]: Entering directory 'D:/git/SimpleSimulationEngine/cpp/Build'
C:/MinGW/bin/make -f common\algorithms\CMakeFiles\algorithms.dir\build.make common/algorithms/CMakeFiles/algorithms.dir/depend
make[2]: Entering directory 'D:/git/SimpleSimulationEngine/cpp/Build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" D:\git\SimpleSimulationEngine\cpp D:\git\SimpleSimulationEngine\cpp\common\algorithms D:\git\SimpleSimulationEngine\cpp\Build D:\git\SimpleSimulationEngine\cpp\Build\common\algorithms D:\git\SimpleSimulationEngine\cpp\Build\common\algorithms\CMakeFiles\algorithms.dir\DependInfo.cmake --color=
make[2]: Leaving directory 'D:/git/SimpleSimulationEngine/cpp/Build'
C:/MinGW/bin/make -f common\algorithms\CMakeFiles\algorithms.dir\build.make common/algorithms/CMakeFiles/algorithms.dir/build
make[2]: Entering directory 'D:/git/SimpleSimulationEngine/cpp/Build'
[ 1%] Building CXX object common/algorithms/CMakeFiles/algorithms.dir/main.cpp.obj
cd /d D:\git\SimpleSimulationEngine\cpp\Build\common\algorithms && C:\MinGW\bin\g++.exe #CMakeFiles/algorithms.dir/includes_CXX.rsp -Wall -std=c++17 -g -Og -fPIC -fno-strict-aliasing -Wno-maybe-uninitialized -Wno-char-subscripts -Wno-write-strings -Wno-format -Wno-parentheses -Wno-unused-but-set-variable -Wno-narrowing -Wno-unused-result -Wno-sign-compare -Wno-strict-aliasing -Wno-unused-variable -Wno-unused-value -Wno-comment -Wno-misleading-indentation -Werror=return-type -o CMakeFiles\algorithms.dir\main.cpp.obj -c D:\git\SimpleSimulationEngine\cpp\common\algorithms\main.cpp
In file included from c:\mingw\x86_64-w64-mingw32\include\stdlib.h:9:0,
from c:\mingw\include\c++\6.1.0\cstdlib:75,
from D:/git/SimpleSimulationEngine/cpp/common/utils/testUtils.h:6,
from D:\git\SimpleSimulationEngine\cpp\common\algorithms\main.cpp:2:
c:\mingw\x86_64-w64-mingw32\include\crtdefs.h:35:1: error: '__MINGW_EXTENSION' does not name a type
__MINGW_EXTENSION typedef unsigned __int64 size_t;
^~~~~~~~~~~~~~~~~
c:\mingw\x86_64-w64-mingw32\include\crtdefs.h:45:1: error: '__MINGW_EXTENSION' does not name a type
__MINGW_EXTENSION typedef __int64 ssize_t;
^~~~~~~~~~~~~~~~~
c:\mingw\x86_64-w64-mingw32\include\crtdefs.h:52:9: error: 'size_t' does not name a type
typedef size_t rsize_t;
^~~~~~
c:\mingw\x86_64-w64-mingw32\include\crtdefs.h:62:1: error: '__MINGW_EXTENSION' does not name a type
__MINGW_EXTENSION typedef __int64 intptr_t;
^~~~~~~~~~~~~~~~~
c:\mingw\x86_64-w64-mingw32\include\crtdefs.h:75:1: error: '__MINGW_EXTENSION' does not name a type
__MINGW_EXTENSION typedef unsigned __int64 uintptr_t;
I'm not sure my MinGW environment and libraries are setup properly, since I tried some older minGW version before, than I reinstalled it. Also I have installed MSYS2 in an atempt to install GLEW. But I do not use MSYS2 now, I use some stand alone instalation of MinGW and cmake.
There is nothing special in my CMakeLists.txt
cmake_minimum_required ( VERSION 2.8 )
project ( SimpleSimulationEngine )
set(default_build_type Release)
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++17" )
SET( IGNORE_WARRNING_FLAGS "-Wno-maybe-uninitialized -Wno-char-subscripts -Wno-write-strings -Wno-format -Wno-parentheses -Wno-unused-but-set-variable -Wno-narrowing -Wno-unused-result -Wno-sign-compare -Wno-strict-aliasing -Wno-unused-variable -Wno-unused-value -Wno-comment -Wno-misleading-indentation " )
SET( WARRNING_TO_ERROR "-Werror=return-type")
SET( AXULIARY_COMPILE_FLAGS "-g -Og -fPIC -fno-strict-aliasing ")
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${AXULIARY_COMPILE_FLAGS} ${IGNORE_WARRNING_FLAGS}
message ( "CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} )
When I check my environment variables/path there does not seem to be anything fishy (like two versions of MinGW)

Changed include path behaviour from CMake 2.8.x to 3.9.x

I have a fairly large cmake project that exhibits a compiler error when I use Makefiles generated by CMake 3.9.x:
Scanning dependencies of target client
[ 21%] Building C object
src/lib/client/CMakeFiles/client.dir/client.c.o
In file included from <command-line>:0:0:
/usr/include/stdc-predef.h:40:1: fatal error: compiler.h: No such file or directory
#endif
^
This works properly when using Makefiles generated by CMake 2.8.x. Digging in a bit, I can see that a change was made to the flags.make file generated by CMake between these two versions. The older version used to put -I (include) options in the C_FLAGS variable defined in that file:
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile C with /usr/lib64/ccache/cc
C_FLAGS = -Wall -Wextra -Werror -Wformat=2 -Wundef -mcx16 -Werror-implicit-function-declaration -Wno-unused-parameter -D_GNU_SOURCE -include compiler.h -D__BUG_OUT_AUX=pd_trc_ftl -Wstrict-prototypes -Wdeclaration-after-statement -Wno-tautological-compare -g -I/.../src/lib/client ... -fPIC
C_DEFINES = -DBUILD_NUMBER=\"whatever\" -DBUILD_VERSION=\"1.66.0\" -DCOMMIT_HASH=\"f9bf1c93682f\" -DPDDEBUG -D_FILE_OFFSET_BITS=64
In later versions of CMake the -I options are broken out into their own C_INCLUDES variable:
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.9
# compile C with /usr/lib64/ccache/cc
C_FLAGS = -Wall -Wextra -Werror -Wformat=2 -Wundef -mcx16 -Werror-implicit-function-declaration -Wno-unused-parameter -D_GNU_SOURCE -include compiler.h -D__BUG_OUT_AUX=pd_trc_ftl -Wstrict-prototypes -Wdeclaration-after-statement -Wno-tautological-compare -g -fPIC
C_DEFINES = -DBUILD_NUMBER=\"whatever\" -DBUILD_VERSION=\"1.66.0\" -DCOMMIT_HASH=\"f9bf1c93682f\" -DPDDEBUG -D_FILE_OFFSET_BITS=64
C_INCLUDES = -I/.../src/lib/client ...
However, in both cases, the including file - build.make - uses only the $(C_DEFINES) $(C_FLAGS), omitting the $(C_INCLUDES) in the newer model:
...
# Include the compile flags for this target's objects.
include src/lib/client/CMakeFiles/client.dir/flags.make
src/lib/client/CMakeFiles/client.dir/client.c.o: src/lib/client/CMakeFiles/client.dir/flags.make
src/lib/client/CMakeFiles/client.dir/client.c.o: ../src/lib/client/client.c
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/.../cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object src/lib/client/CMakeFiles/client.dir/client.c.o"
cd /.../cmake-build-debug/src/lib/client && /.../contrib/cc/cgcc.sh /.../cmake-build-debug /usr/lib64/ccache/cc sparse ON /.../client-project CMakeFiles/client.dir/client.c.o $(C_DEFINES) $(C_FLAGS) -o CMakeFiles/client.dir/client.c.o -c /.../src/lib/client/client.c
...
Is this a bug in CMake 3.9.x? Has anyone else experienced anything like this when upgrading CMake?
I believe it's possible that we've always done something wrong in our CMakeLists.txt files that just happened to work in the older versions, but that when we upgraded to CMake 3.9.x, suddenly the problem is manifest. Hoping someone has had this issue and figured out what they did wrong.
Thanks in advance!

Android Studio NDK iostream file not found and does not able to enable neon in CMakeList

I am new on using NDK and CMake, and got the problem when I compiled my C++ library, Android studio keeps compiling that
Error:(28, 2) error: "NEON support not enabled"
Error:error: 'neon_vector_type' attribute is not supported for this
target
Error:(17, 10) fatal error: 'iostream' file not found
I have see some solution saying that I should add APP_STL := stlport_static in Application.mk
However, I am using CMakeLists instead of Application.mk.
So I added -DANDROID_ARM_NEON=TRUE -DAPP_STL=stlport_static on CMAKE_C_FLAGS, but it still produce the same error
This is my CMakeLists
set (pathToProject /home/user/project)
set (pathToOpenCv /home/user/project/OpenCV330)
FILE(GLOB_RECURSE cppfiles src/main/cpp/src/*)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED on)
find_package(OpenMP)
add_definitions(-fPIC)
add_definitions(-fopenmp)
add_definitions(-Ofast)
add_definitions(-DANDROID_STL=c++_shared)
add_definitions(-flax-vector-conversions)
add_definitions(-fopenmp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive -DANDROID_ARM_NEON=TRUE -DAPP_STL=stlport_static")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
include_directories(${pathToProject}/app/src/main/cpp/src/include)
include_directories(${pathToOpenCv}/sdk/native/jni/include)
add_library(lib_opencv SHARED IMPORTED)
add_library(native-lib SHARED src/main/cpp/native-lib.cpp )
add_library(mylibrary SHARED ${cppfiles} )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
find_library(log-lib log)
target_link_libraries(
native-lib
${log-lib}
lib_opencv
mylibrary
)
Updated:
I also found 1 more compiling message, looks like the -std=c++11 is not actually applied while compiling
[10/11] Building CXX object CMakeFiles/mylibrary.dir/src/main/cpp/src/yuv420sp.cpp.o
FAILED: /home/user/Downloads/android-ndk-r14b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=i686-none-linux-android --gcc-toolchain=/home/user/Downloads/android-ndk-r14b/toolchains/x86-4.9/prebuilt/linux-x86_64 --sysroot=/home/user/Downloads/android-ndk-r14b/platforms/android-24/arch-x86 -Dmylibrary_EXPORTS -I../../../../src/main/cpp/include -I/home/user/git/project/OpenCV-android-sdk3.3.0/sdk/native/jni/include -I../../../../src/main/cpp/build/include -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/mylibrary.dir/src/main/cpp/src/object_wrap.c.o -MF CMakeFiles/mylibrary.dir/src/main/cpp/src/object_wrap.c.o.d -o CMakeFiles/mylibrary.dir/src/main/cpp/object_wrap.c.o -c /home/user/git/project/app/src/main/cpp/object_wrap.c
In file included from /home/user/git/project/app/src/main/cpp/src/object_wrap.c:209:
/home/user/git/project/app/src/main/cpp/src/object.h:17:10: fatal error: 'iostream' file not found
#include <iostream>
^
Also tried to set the flag in app gradle, still not working
externalNativeBuild {
cmake {
cppFlags "-std=c++11", "-Wno-error=format-security"
arguments "-DANDROID_STL=gnustl_static", "-DANDROID_ARM_NEON=TRUE"
}
}
if I set -DANDROID_STL=stlport_static, it will give two more error
Error:(25, 10) fatal error: 'thread' file not found
Error:(424, 14) fatal error: 'array' file not found
After changing the to gcc toolchain in gradle, problem solved.
arguments "-DANDROID_TOOLCHAIN=gcc","-DANDROID_ARM_NEON=TRUE" ,"-DANDROID_STL_FORCE_FEATURES=OFF"
cppFlags "-std=gnu++11", "-Wno-error=format-security"