Cmake在Mac OSX上使用Frameworks进行编译,并处理.m / .mm等.cpp文件

时间:2010-09-22 14:53:02

标签: c++ objective-c cmake

我正在寻找一个提示让以下工作,这是我的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}
)

我要求在OSX上.cpp文件被视为.mm文件(但在Windows上,而不是),我可以加载核心基础等框架...我是一个完整的cmake新手,所以我可以甚至开始知道从哪里开始,但我希望我能以某种方式朝着正确的方向前进,目前的输出是:

$ 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

1 个答案:

答案 0 :(得分:6)

一旦我理解了应该在幕后发生的事情,这就变得相当容易了:

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

告诉gcc您要将-x language中的语言属性(man gcc)设置为Objective-C++

您也可以使用以下内容对单个文件执行此操作:

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

我和两者都取得了不同的成功,可能突出了一些我对CMake并不完全了解的事情。