CMake为Xcode目标与正常目标

时间:2013-06-23 02:52:18

标签: xcode c++11 cmake

我有这个简单的C ++ 11代码。

#include <vector>
#include <iostream>
#include <memory>

using namespace std;

class A
{
    int x;
public:
    A() {}
    ~A() {}
    A(A& a) {}
    A(int x) {this->x = x;}
    int get() {return x;}
};

int main()
{
    vector<unique_ptr<A>> v;
    auto a = new A(10);
    unique_ptr<A> pa(a);
    v.push_back(move(pa)); // move(pa);

    auto a2 = new A(20);
    unique_ptr<A> pb(a2);
    v.push_back(move(pb)); // move(pa);

    for (auto& i: v)
    {
        cout << i->get();
    }
}

我正在尝试使用Xcode和clang ++上的CMake设置构建此代码。

这是CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8)
project( testit )

set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")

set(testit
    testit.cpp
    )

add_executable(gv ${testit})

clang ++ build。

  1. mkdir buildcd build
  2. export CC=/usr/bin/clang
  3. export CXX=/usr/bin/clang++
  4. cmake ..
  5. make
  6. 我可以得到工作正常的二进制文件。

    Xcode 4.5目标

    1. 同一步骤1-3
    2. cmake .. -G Xcode
    3. xcodebuild
    4. 编译工作正常,但我在构建时遇到错误Undefined symbol。 我在CMake set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")中使用此命令设置了C ++ 11。

      Undefined symbols for architecture x86_64:
        "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
            void std::__1::vector<std::__1::unique_ptr<A, std::__1::default_delete<A> >, std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > > >::__push_back_slow_path<std::__1::unique_ptr<A, std::__1::default_delete<A> > >(std::__1::unique_ptr<A, std::__1::default_delete<A> >&&) in testit.o
        "std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int)", referenced from:
            _main in testit.o
        "std::__1::cout", referenced from:
            _main in testit.o
      ld: symbol(s) not found for architecture x86_64
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      

      可能出现什么问题?

      ADDED

      从这个链接看来,CMake的xbuildcode的目标设置似乎是错误的: http://www.executionunit.com/blog/2012/10/27/xcode-std-link-errors/

      ADDED

      我可以解决此问题,但我必须使用CMakeLists.txt文件所在的同一目录。

      Enter image description here

      当我执行CMake .. -G XCode时,我又遇到了另一个错误。

      === BUILD AGGREGATE TARGET ZERO_CHECK OF PROJECT XcodeTest WITH THE DEFAULT CONFIGURATION (Debug) ===
      Check dependencies
      
      PhaseScriptExecution "CMake Rules" build/XcodeTest.build/Debug/ZERO_CHECK.build/Script-BED7FB205C634C34A1ACD293.sh
          cd /Users/smcho/Desktop/cmake
          /bin/sh -c /Users/smcho/Desktop/cmake/build/XcodeTest.build/Debug/ZERO_CHECK.build/Script-BED7FB205C634C34A1ACD293.sh
      echo ""
      
      make -f /Users/smcho/Desktop/cmake/build/CMakeScripts/ReRunCMake.make
      make[1]: *** No rule to make target `/Users/smcho/Desktop/cmake/build/CMakeFiles/2.8.10.2/CMakeCCompiler.cmake', needed by `CMakeFiles/cmake.check_cache'.  Stop.
      make: *** [/Users/smcho/Desktop/cmake/build/CMakeFiles/ZERO_CHECK] Error 2
      Command /bin/sh failed with exit code 2
      
      ** BUILD FAILED **
      
      The following build commands failed:
          PhaseScriptExecution "CMake Rules" build/XcodeTest.build/Debug/ZERO_CHECK.build/Script-BED7FB205C634C34A1ACD293.sh
      

1 个答案:

答案 0 :(得分:4)

有两个问题。

链接器选项

正如弗雷泽指出的那样,问题来自链接器中的设置。

...    
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
...

Add_subdirectory

我不确定为什么会这样,但我需要在主CMakeLists.txt文件中使用add_subdirectory(src),并在该src目录中创建另一个CMakeLists.txt文件以便运行在构建目录中进行CMake。没有它,我必须在CMakeLists.txt文件所在的同一目录中运行CMake。请参阅Stack Overflow问题 CMake Xcode generator creates a project that cannot build

这是src目录中的CMakeLists.txt:

set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
file ( GLOB SRCS *.cpp )
add_executable( program ${SRCS})

这是主目录中的CMakeLists.txt:

project( XcodeTest )
cmake_minimum_required( VERSION 2.6 )

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")

add_subdirectory(src)

我从YouTube视频中得到了提示: CMake XCode Demo

Enter image description here