Cmake compile with Frameworks on Mac OSX and treat .cpp files like .m/.mm


I'm looking for a tip to get the following to work, here is my CMakeLists.txt

# cmake_minimum_required(2.8.2)

project(boilerplate)

# base files
set(src_files
  src/greet.h
  src/main.cpp
)

# if on OSX, these files are needed
if(APPLE)

  SET(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -w")

  set(src_files
    ${src_files}
    src/mac/greet.mm
    src/mac/greeting.h
    src/mac/greeting.m
  )
endif()

# if on windows, these files are needed
if(WIN32)
  set(src_files
    ${src_files}
    src/win/greet.cpp
  )
endif()

add_executable(greeting
  ${src_files}
)

I require that on OSX the .cpp files are treated like .mm files (but on Windows, not) and that I can load the core foundation, etc frameworks... I'm a complete cmake newbie, so I can't even begin to know where to start, but I hope I'm somehow in the right direction, current output is:

$ cmake CMakeLists.txt && make
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/leehambley/Projects/watched.it-client
Scanning dependencies of target greeting
[ 33%] Building CXX object CMakeFiles/greeting.dir/src/mac/greet.o
Linking CXX executable greeting
Undefined symbols:
  "greet()", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [greeting] Error 1
make[1]: *** [CMakeFiles/greeting.dir/all] Error 2
make: *** [all] Error 2
1
- - Source

Answers

answered 8 year ago Lee Hambley #1

This turned out to be rather easy once I understood what was supposed to be happening under the hood:

set(CMAKE_CXX_FLAGS "-x objective-c++")

Which tells gcc that you want to set the language property (-x language, in man gcc) to Objective-C++.

You can also do this for individual files with:

set_source_files_properties(${SOURCE_FILES} PROPERTIES 
                            COMPILE_FLAGS "-x objective-c++")

I've had mixed success with both, probably highlighting some of the things I don't fully understand about CMake.

comments powered by Disqus