我有一段简单的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文件夹并进入该文件夹
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
-stdlib=libc++
参数?set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
- Fraserwww.executionunit.com
的链接已经失效(“未找到。无法定位所请求的项目。”)。 - Peter Mortensen