我有这个简单的 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})
铿锵++构建。
mkdir build
和cd build
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
cmake ..
make
我可以得到运行良好的二进制文件。
Xcode 4.5 目标
- 相同的步骤 1-3
cmake .. -G Xcode
xcodebuild
编译工作正常,但我在构建中遇到错误并Undefined symbol
出现错误。我在 CMake 中使用此命令设置 C++11 set(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