21

I have a pure C++11 DLL (No dependencies of any kind) I have been able to compile on Linux and Windows for some time now using CMake to generate the project files and make/MSVC to compile in each respective native system.

I want to compile on OSX now and I have been having a lot of problems getting CMake to set the correct project settings in XCode to compile the DLL.

Software versions:

XCode v5.0
CMake v2.8.12

The relevant CMake script code:

# Set output directory if Apple OSX:
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    message("CMAKE HAS DETECTED A OSX SYSTEM - BUILDING FOR XCODE!")

    set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
    set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++0x -stdlib=libc++")

    IF(CMAKE_BUILD_TYPE MATCHES Release)
        SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin/OSX/Release)
    ENDIF(CMAKE_BUILD_TYPE MATCHES Release)
    IF(CMAKE_BUILD_TYPE MATCHES Debug)
        SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin/OSX/Debug)
    ENDIF(CMAKE_BUILD_TYPE MATCHES Debug)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

However the settings do not correctly come through into the XCode project file:

enter image description here

You can see that the CMake commands make their way into the 'Other C++ Flags' area. But XCode will still fail to compile. However, if I change the 'C++ Standard Library' variable to 'libc++' it will compile fine.

Note: I could post the compile error logs however I think that the above correctly identifies the root cause - I just need to know what CMake command actually sets the correct XCode property above.

S.Richmond
  • 11,412
  • 6
  • 39
  • 57
  • Probably you have already looked at this http://stackoverflow.com/questions/7879630/using-c0x-in-xcode-4-2-project-via-cmake . Anyway, it looks that xcode in the newer version with Maverks turns on C++11 by default. – lib Nov 09 '13 at 13:28
  • Yep. Doesn't help sadly. – S.Richmond Nov 10 '13 at 02:57

2 Answers2

9

For a minimal test project the following does the job:

SET(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")

Maybe it's interfering with the following line:

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++0x -stdlib=libc++")

Also one remark on this line, is there the ${CMAKE_C_FLAGS} on intention, or did you really mean ${CMAKE_CXX_FLAGS}?

Mario
  • 791
  • 7
  • 19
  • I've tried disabling all other related SET functions to no avail. Can you confirm what version of CMake and XCode you're running? I'm running CMake v2.8.12 and XCode v5.0. – S.Richmond Oct 15 '13 at 23:14
5

you can use set_property

macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY}
    ${XCODE_VALUE})
endmacro (set_xcode_property)

set_xcode_property(${your_target_name} CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set_xcode_property(${your_target_name} CLANG_CXX_LIBRARY "libc++")
zhang xiang
  • 91
  • 1
  • 3
  • Hrm. I'll definitely give this go very soon. Could you provide a little info on where set_property() is documented and why one would use it instead of just set()? – S.Richmond Feb 04 '14 at 07:03
  • Why we just can not do this? `set_property(TARGET ${your_target_name} PROPERTY XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x") set_property(TARGET ${your_target_name} PROPERTY XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")` – woland Oct 21 '21 at 20:07