Compiling for iOS with CMake - c++

I've compiled a C++ static library by using CMake as my building tool and I want to link it to my iOS app.
I created a simple 'Empty' application in Xcode and linked my library called libengine.a to it.
I tried to compile my iOS project and the linker gave me this warning:
ignoring file /Users/.../build/engine/libengine.a,
file was built for archive which is not the architecture being linked (i386):
/Users/.../build/engine/libengine.a
As I understand it, I need to compile my library for ARM processors. The problem is I don't know how.
I think CMake really lacks good tutorials.
Anyways, my CMake scripts are attached below.
Any help would be greatly appreciated.
Thanks, Tal.
Here is my main CMake script:
cmake_minimum_required(VERSION 2.8)
project(movie-night)
if (DEFINED PLATFORM)
include(toolchains/ios.cmake)
endif()
add_definitions(-Wall)
set(DEBUG)
if (DEFINED DEBUG)
add_definitions(-g)
endif()
if (DEFINED RELEASE)
add_definitions(-O3)
endif()
add_subdirectory(engine)
add_subdirectory(ui)
add_subdirectory(test)
Here is my toolchains/ios.cmake file:
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm)

Just use this toolchain file: http://code.google.com/p/ios-cmake/ and use it as
cmake -DCMAKE_TOOLCHAIN_FILE=path_to_your_toolchain_file
Then, in CMakeLists.txt:
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch armv7")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch armv7")

By following cmake-toolchains documentation I did like below:
cmake -G Xcode -B build \
-DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_Swift_COMPILER_FORCED=true \
-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
Note: That assignment CMAKE_OSX_DEPLOYMENT_TARGET=11.0 is not a mistake when targeting iOS.

I've been using an updated version of the iOS CMake toolchain for quite a while: https://github.com/leetal/ios-cmake
This creates an Xcode project which you can drag into an existing iOS project if necessary.
I wrote a blog post with more details: https://blog.tomtasche.at/2019/05/how-to-include-cmake-project-in-xcode.html

There is a second version of iOS.cmake located at:
https://ceres-solver.googlesource.com
Note: You may find that both versions of iOS.cmake will only build x86 versions of your project. CMake now sets CMAKE_OSX_SYSROOT to the (latest) Mac OS X SDK available on your system. You can fix this by modifying your copy of iOS.cmake to always set CMAKE_OSX_SYSROOT. You can do this by commenting out a couple of lines your copy of iOS.cmake:
# -- Under CMake 3.4.2, CMAKE_OSX_SYSROOT is automatically defined to point to the latest Mac OS X SDK. --
# -- So, the iOS SDK is never found. Grab the correct CMAKE_OS_SYSROOT and ignore any prior setting. --
# If user did not specify the SDK root to use, then query xcodebuild for it.
# if (NOT CMAKE_OSX_SYSROOT)
execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
message (STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
message (STATUS "be sure the previous line points to the correct SDK")
# endif ( )

Related

Missing CMAKE_ASM_NASM_COMPILER when compiling gRPC with MS visual studio

I am trying to build gRPC C++ (1.48.0) with Visual Studio 2022 on Windows 10. It's a CMake build (cmake 3.22.22011901-MSVC_2)
I was able to build everything else but am stuck at BoringSSL. The relevant CMakeList is trying to enable_language(ASM_NASM). Context below:
if(NOT OPENSSL_NO_ASM)
if(UNIX)
enable_language(ASM)
# Clang's integerated assembler does not support debug symbols.
if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
endif()
# CMake does not add -isysroot and -arch flags to assembly.
if(APPLE)
if(CMAKE_OSX_SYSROOT)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
endif()
foreach(arch ${CMAKE_OSX_ARCHITECTURES})
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
endforeach()
endif()
else()
set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
enable_language(ASM_NASM)
endif()
endif()
It gives me CMake Error: "No CMAKE_ASM_NASM_COMPILER could be found."
I don't know enough about compilers / assemblers, and why boringSSL would need a specific one (which none of the other modules needed including gRPC).
What is the recommended way to fix this?
To answer at least some of my questions for my future self, and others who are at a similar point in the journey.
NASM is an assembly compiler (assembler). BoringSSL has some assembly language code, which is why it needs an assembly compiler (and gRPC or other modules don't). I'll let someone else opine on why NASM and not some other assembler.
To fix the issue, you have to download/install the relevant NASM executable from here. I found it easier to download the exe, place it in a folder, add that folder to the PATH and set another env variable ASM_NASM with the name of nasm.exe.
Once I did that, boringSSL and the whole gRPC compiled quite smoothly.

Clion with CMake can not see header files of OpenGL on MacOS [duplicate]

How can you link GLEW to a project with CMake?
We've been trying to link GLEW to our project using CMake for at least 3 hours without any success so any help is accepted.
I'm using the FindGLEW.cmake which comes with CMake 3.1.0
CMakeLists.txt
find_package(GLEW REQUIRED)
if (GLEW_FOUND)
include_directories($(GLEW_INCLUDE_DIRS))
endif()
Environment Variables
I'm using MinGW w64 to compile the sources and we successfully linked GLFW and GLM just by copying the includes and libs to their respective folders, but after doing the same with GLEW, CMake still couldn't find it.
Sorry if I wasn't clear enough while formulating the question. I will provide any additional information required.
Edit: I've managed to link the header files by specifying their location in the CMake Cache file, though I'm getting undefined reference to glew functions like glewInit().
Typical CMake scripts like FindGLEW will define variables that specify the paths and files that your project needs. If the script can't automatically identify the correct paths (usually because of nonstandard install location, which is fine), then it leaves these variables up to you to fill in.
With command line CMake, you use the -D flag to define and set the value of a given variable. Other CMake interfaces, like CMake-gui or an IDE integration, give you this ability some other way.
However you do it, you can also modify the cache directly (CMakeCache.txt) and see what CMake is using in there or just clear the cache altogether. You'll have to rerun CMake for it to pick up your changes.
When it comes to linking, that's when you need to tell CMake which libs to link. Use the link_libraries command with what the automated script gives you.
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})
Other answers do obviously work, but the target based style of cmake makes it even easier since the GLEW find module defines the imported target GLEW::GLEW. All you need is:
find_package(GLEW REQUIRED)
target_link_libraries(YourTarget GLEW::GLEW)
YourTarget is the target that you created with add_executable or add_library. No need to explicitly add include directories, they are added automatically by linking the targets.
The secret of find_package(GLEW) is in FindGLEW.cmake file with cmake install.
find_path(GLEW_INCLUDE_DIR GL/glew.h)
find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATH_SUFFIXES lib64)
The find_path and find_library commands find paths in standard system paths. If you want them to find paths in user defined directories, you should tell them.
For example:
set(CMAKE_PREFIX_PATH "d:/libs/glew-1.10.0")
set(CMAKE_LIBRARY_PATH "d:/libs/glew-1.10.0/lib/Release/Win32/")
find_package(GLEW REQUIRED)
Reference:
http://www.cmake.org/cmake/help/v3.0/command/find_path.html
http://www.cmake.org/cmake/help/v3.0/command/find_library.html
I was struggling hard to link glew to cmake through command line on mac. This might be helpful but I am not sure :) I will walk you through step by step of what I have done.
I installed Cmake source from the web.
Then I went inside the cmake folder in terminal and typed
./bootstrap && make && make install
(this will install cmake command line tools on our OS platform)
I have some exercise files. I want cmake to generate xcode files for me for all those exercise files (ex. triangles.cpp, shader.cpp etc) So i made a directory inside exercise files folder.
$ mkdir xcode
$ cd xcode
$ cmake -G "Xcode" ..
At this point, Cmake suppose to install all xcode files that included correct libraries. But there was an error :
$ cmake -G "Xcode" ..
CMake Warning (dev) at CMakeLists.txt:3 (cmake_minimum_required):
Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.
This warning is for project developers. Use -Wno-dev to suppress it.
system name is: Darwin-14.1.0
system processor is: x86_64
-- Could NOT find GLEW (missing: GLEW_INCLUDE_DIR GLEW_LIBRARY)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Using Cocoa for window creation
-- Using NSGL for context creation
-- Building GLFW only for the native architecture
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLEW_LIBRARY
linked by target "TextureLoader" in directory /Users/Mydir/Desktop/Exercise/Exercise Files
-- Configuring incomplete, errors occurred!
Then to make sure I have installed GLEW and all its libraries correctly, I ran
$brew install glew
Yes, I have installed glew already but it was NOT linked. See the Warning below:
Warning: glew-1.12.0 already installed, it's just not linked
Then I ran the following commands:
$ brew unlink glew
$ brew link glew
And I have solved the error. So just make sure that you have linked glew. Hope this helps.
Happy Coding :)
Finally I found a simple and short CMakeLists which works if you have installed everything in default paths.(openGL, glfw and glew)
cmake_minimum_required(VERSION 3.3)
project(openGL_tutorial)
find_package(OpenGL REQUIRED)
if(NOT OPENGL_FOUND)
message("ERROR: OpenGL not found")
endif(NOT OPENGL_FOUND)
set(GL_LIBRARY GL GLU X11)
add_executable(openGL_tutorial main.cpp)
target_link_libraries(openGL_tutorial glfw GLEW libGLEW.so libGLU.so libGL.so)
For what it is worth, in 2023, this works for me, on macOS, with GLEW, GLFW, and CMake installed using Homebrew:
cmake_minimum_required(VERSION 3.10)
project(Project)
add_executable(Project main.cpp)
find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)
target_link_libraries(Project glfw GLEW::glew)

FreeType2 Cross-compiling for Android

I was able to compile Freetype2 library for arm, arm64, x86, x86_64 architectures. These are steps I done to compile it.
Created standalone toolchains for architectures mentioned above.
make_standalone_toolchain.py \
--arch arm64 \
--api 26 \
--stl=libc++ \
--install-dir=my-arm64-toolchain
Set some envirement variables
export PATH=$PATH:`pwd`/my-toolchain/bin
target_host=aarch64-linux-android
export AR=$target_host-ar
export AS=$target_host-as
export CC=$target_host-gcc
export CXX=$target_host-g++
export LD=$target_host-ld
export STRIP=$target_host-strip
export CFLAGS="-fPIE -fPIC"
export LDFLAGS="-pie"
Configure freetype for compilation
./configure --host=aarch64-linux-android --prefix=/home/freetype-arm64 --without-zlib --without-harfbuzz --with-png=no
And finally make && install
Compilation was successfull and I was able to get Static freetype library.
I added library to my android studio libs folder.
This is folder structure for library:
libs---freetype
|---${ANDROID_ABI}
|---include
|---freetype2
|---freetype
---|Bunch of header files
|---ftbuild.h
|---lib
|---libfreetype.a
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
add_definitions("-DGLM_FORCE_SIZE_T_LENGTH -DGLM_FORCE_RADIANS")
add_subdirectory(src/main/cpp/glm)
add_library(freetype STATIC IMPORTED)
set_target_properties(freetype PROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/libs/freetype/${ANDROID_ABI}/lib/libfreetype.a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wall")
add_library(native-lib SHARED
src/main/cpp/native-lib.cpp
src/main/cpp/graph.cpp
src/main/cpp/text.cpp
src/main/cpp/graphDataWorker.cpp
)
target_include_directories(native-lib PRIVATE ${PROJECT_SOURCE_DIR}/libs/freetype/${ANDROID_ABI}/include/freetype2)
target_link_libraries(native-lib
android
freetype
GLESv2
EGL
glm
atomic
log
OpenSLES
)
The problem is that I can use library inside my code and android studio is not giving me any errors, but then i try to run application it gives me this error:
linker command failed with exit code 1 (use -v to see invocation)
PS: I Can't see freetype headers in android studio file tree aswell, but can see other libraries.
Question: Am I importing library in a wrong way or this is cross-compilation issue and if so how to do it properly?
I done a lot of research on this issue and was unable to find a solution
Any help will be appreciated.
Unless you mixed paths when installing your build of FreeType2, it seems that the compilation of the library is incorrect, since the linker complains about the library being for x86_64 architecture.
On Linux, usually you can check the architecture the library is built for with command file. Another way to check the architecture is to use the objdump tool that is shipped with Android NDK (if I remember correctly it is generated with a toolchain as well). Its name follows the same pattern than the compiler/linker/... tools, $target_host-objdump.
I would recommend to compile FreeType2 using CMake instead of autotools, at least in my case I successfully did it that way.
Here is a sample of CMake configuration I wrote to cross-compile FreeType2 for Android. It is a little complex because it is part of a project where I build several libraries.
The idea is that my CMake configuration uses ExternalProject to download and compile Freetype. My top-level CMakeLists.txt contains:
# CMakeLists.txt
set(DEVENV_CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
-DCMAKE_C_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_C_FLAGS_${BUILD_TYPE}=${CMAKE_C_FLAGS_${BUILD_TYPE}}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_CXX_FLAGS_${BUILD_TYPE}=${CMAKE_CXX_FLAGS_${BUILD_TYPE}}
-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
)
if(BUILD_SHARED_LIBS)
list(APPEND DEVENV_CMAKE_ARGS -DBUILD_STATIC_LIBS=OFF -DLIBTYPE=SHARED)
else()
list(APPEND DEVENV_CMAKE_ARGS -DBUILD_STATIC_LIBS=ON -DLIBTYPE=STATIC)
endif()
if(DEFINED ANDROID_TOOLCHAIN_DIR)
get_filename_component(_toolchain_dir ${ANDROID_TOOLCHAIN_DIR} ABSOLUTE)
list(APPEND DEVENV_CMAKE_ARGS -DANDROID_TOOLCHAIN_DIR=${_toolchain_dir})
endif()
if(DEFINED CMAKE_ANDROID_ARCH_ABI)
list(APPEND DEVENV_CMAKE_ARGS -DCMAKE_ANDROID_ARCH_ABI=${CMAKE_ANDROID_ARCH_ABI})
endif()
if(CMAKE_TOOLCHAIN_FILE)
get_filename_component(_toolchain_file ${CMAKE_TOOLCHAIN_FILE} ABSOLUTE)
list(APPEND DEVENV_CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${_toolchain_file})
endif()
add_subdirectory(freetype)
So the variable DEVENV_CMAKE_ARGS contains appropriate CMake arguments for cross-compiling that reflect my current CMake configuration. Under directory freetype, the CMakeLists.txt contains:
# freetype/CMakeLists.txt
project(freetype)
include(ExternalProject)
if(NOT FREETYPE_GIT_REPOSITORY)
set(FREETYPE_GIT_REPOSITORY "https://git.savannah.gnu.org/git/freetype/freetype2.git")
endif()
set(FREETYPE_GIT_REPOSITORY "${FREETYPE_GIT_REPOSITORY}"
CACHE STRING "Git repository for library Freetype."
)
if(NOT FREETYPE_GIT_TAG)
set(FREETYPE_GIT_TAG "VER-2-9")
endif()
set(FREETYPE_GIT_TAG "${FREETYPE_GIT_TAG}"
CACHE STRING "Tag or branch of Git repository to build."
)
message(STATUS "Freetype Git repository: ${FREETYPE_GIT_REPOSITORY}")
message(STATUS "Freetype Git tag: ${FREETYPE_GIT_TAG}")
externalproject_add(freetype
GIT_REPOSITORY "${FREETYPE_GIT_REPOSITORY}"
GIT_TAG "${FREETYPE_GIT_TAG}"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/freetype/src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/freetype/build"
CMAKE_ARGS ${DEVENV_CMAKE_ARGS}
)
Basically this CMakeLists.txt uses ExternalProject to download and compile FreeType2 in the build directory.
And I use a toolchain file for Android:
# android.cmake
set(CMAKE_SYSTEM_NAME "Android")
set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN ${ANDROID_TOOLCHAIN_DIR})
Sorry if it is overly complex; it comes from a project where I was playing with cross-compiling for Android. I hope it helps anyway.

Pjsip on CentOS 7 problems on building CMake project

I'm trying to build my C++ project which includes Pjsip library on Centos 7 using CMake and Qt IDE without success. I built Pjsip for Centos using steps defined in readme.txt :
./configure
make dep && make
After that configured CMakeLists.txt with this configuration:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_INSTALL_PREFIX /opt)
project(hello-pjsip)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DSERVER_REMOTE_LOGGING -ggdb -Wno-unused-local-typedefs")
# Set the compilers as the default GCC for Centos 7 doesn't support all C++11 features
set(CMAKE_CXX_COMPILER /opt/rh/devtoolset-7/root/bin/g++)
set(CMAKE_C_COMPILER /opt/rh/devtoolset-7/root/bin/gcc)
# PJ library
include_directories(~/pjproject/pjlib/include)
link_directories(~/pjproject/pjlib/lib)
include_directories(~/pjproject/pjlib-util/include)
link_directories(~/pjproject/pjlib-util/lib)
include_directories(~/pjproject/pjnath/include)
include_directories(~/pjproject/pjnath/src)
link_directories(~/pjproject/pjnath/lib)
# Source files
set (SOURCE_FILES
main.h
main.cpp
)
# Library files
set (LIBRARY_FILES
pj-x86_64-unknown-linux-gnu
pjnath-x86_64-unknown-linux-gnu
pjlib-util-x86_64-unknown-linux-gnu
)
add_executable(hello-pjsip ${SOURCE_FILES})
target_link_libraries(hello-pjsip ${LIBRARY_FILES})
After running build received these errors:
I tried to recompile pjsip library with different options but no success, same errors every time. Could someone help me?
PJ had bug, solved 5 months ago. This solved problem:
https://trac.pjsip.org/repos/changeset/5599

How to switch between GCC and Clang in Clion from within CMakeLists.txt using windows/cygwin

I put
set(CMAKE_CXX_COMPILER "/usr/bin/clang.exe")
Run/Clean, Run/Build.
I get link errors like:
undefined reference to `std::ios_base::Init::~Init()'
: undefined reference to `__gxx_personality_v0'
Presumably there are other variables to change. Tried adding -lstdc++ to CMAKE_CXX_FLAGS, but no different.
Is there a CLion way as opposed to a CMake way, for example?
Thanks.
Specifying a compiler with CMake is a bit delicate. Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ.
CLion supports method 2 of the CMake FAQ: using -D within the cmake invocation. Setting the variables in CMakeLists.txt has no effect.
On Mac, go to Preferences
On Linux/Windows, go to File | Settings
then Build, Execution, Deployment | CMake | CMake options and enter the text:
-D CMAKE_C_COMPILER=/path/to/c_compiler
-D CMAKE_CXX_COMPILER=/path/to/c++_compiler
See the CLion FAQ for details.
Note also that when you change compilers, you will have to invalidate the CLion cmake cache and restart, see my answer on How to clear CMake cache in Clion?.
EDIT
After I wrote this answer, CLion added support for multiple build directories, as pointed out by #rubenvb in the comments. This is another path to investigate.
In fact the latest version of Clions 2018.2 running on windows 10 environment work with LLVM clang version 6 /6.0.1 or even 7.0 along with GCC mingw x64 win32 specific variant.
linker default is set to GCC not visual studio
I guess it should work on cygwin too with the same setup as the following also tested to work on a number of popular c++ IDE also.
x64 or 32 specific GCC mingw version tested to work on Clions 2018.2
\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0 or mingw-w64\i686-8.1.0-win32-dwarf-rt_v6-rev0
CMake build setup as below
cmake_minimum_required(VERSION 3.10)
project(project_name )
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_COMPILER "c:/llvm/bin/clang.exe")
set(CMAKE_CXX_COMPILER "c:/llvm/bin/clang++.exe")
// target i686-pc-windows-gnu for 32bit
set(CL_COVERAGE_COMPILE_FLAGS "-v -target x86_64-pc-windows-gnu -Wall -Wextra -std=gnu++17")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CL_COVERAGE_COMPILE_FLAGS}" )
add_executable(project_name yourcpp.cpp)