我有这个简单的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
我可以得到工作正常的二进制文件。
cmake .. -G Xcode
xcodebuild
编译工作正常,但我在构建中出现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)
可能有什么问题?
从这个链接看来,目标设置错误来自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++")
...
我不知道为什么会这样,但我需要在主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