#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.
> mkdir build和cd build
> export CC = /usr/bin/clang
>导出CXX = /usr/bin/clang
> cmake ..
>制作
我可以得到工作正常的二进制文件.
Xcode 4.5目标
>同样的步骤1-3
> cmake .. -G Xcode
> xcodebuild
编译工作正常,但我在构建中出现错误,带有未定义的符号错误.
我在CMake集中使用此命令设置C 11(CMAKE_CXX_FLAGS“-std = c 11 -stdlib = libc”).
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)
可能有什么问题?
从这个链接,似乎CMake的xbuildcode的目标设置是错误的:
http://www.executionunit.com/blog/2012/10/27/xcode-std-link-errors/
我可以解决这个问题,但我必须使用CMakeLists.txt文件所在的目录.
当我执行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
解决方法
链接器选项
正如Fraser指出的那样,问题来自链接器中的设置.
... 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文件,以便在build目录中运行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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。