Use CMake to Generate Xcode C++ Project

Meqt

What’s CMake

CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

If you are a C++ programmer, CMake is what you can’t miss, especially when you write cross platform apps.

When Learning a Computer Graphics Course this year, teacher upload an project with Makefile which doesn’t work on my M1 Mac. I use Xcode for C++ IDE, and it’s frustrued set up file path, include path, library path. Then I learned CMake, which you write a CMakeLists.txt to set up your project, like targets, libraries, and it can generate build file for varies developing environment from UNIX Makefile(Which is the default build system), Visual Studio, to Xcode. What’s more, this makes you code portable, as you can’t send you Xcode Project to a friend who is a Windows user.

Besides, some library won’t provide you a compile version of the library, instead, you can download source code, and compile on your own computer. For example:

1
2
3
4
5
6
7
git clone https://github.com/oatpp/oatpp.git
cd oatpp/

mkdir build && cd build

cmake ..
make install

CMake Learning Material

CMake

an CMakeList.txt example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cmake_minimum_required(VERSION 3.15)
project(Tutorial VERSION 1.0)

add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,CNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
target_compile_options(tutorial_compiler_flags INTERFACE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
"$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
configure_file(TutorialConfig.h.in TutorialConfig.h)

if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)

target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)

Build Project

1
2
3
4
mkdir Step1_build
cd Step1_build
cmake ../Step1
cmake --build .

Simplest CMakeLists.txt Example

1
2
3
4
5
6
7
add_library(MathFunctions mysqrt.cxx)

target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(MathFunctions tutorial_compiler_flags)

Generate Xcode Project

The default generator is UNIX Make, you can change the generator to Xcode Using at newly maked build_xcode directory,

1
cmake ${CMakeLists.txt file path} -G Xcode

Remember, after you add files to your Xcode project, it hasn’t been add to your CMakeLists.txt, if you regenerate xcode project with cmake, problems gonna happen. You can fix it be edit your CMakeLists.txt, emm, it truly tedious add your file in CMakeLists.txt every time, but I haven’t found a better way yet.

Xcode C++ Error

Date: Sun Dec 18 11:22:30 CST 2022
After I Updated Xcode 14.2 Last Night, when I reopen my c++ Project, this problem happened.
error
Problem fixed after I regenerate my xcode project in directory build_xode:

1
2
rm -r *
cmake .. -G Xcode
  • Post title:Use CMake to Generate Xcode C++ Project
  • Post author:Meqt
  • Create time:2022-12-17 01:42:13
  • Post link:https://meqtmac.github.io/2022/12/17/cmake_xcode/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.