Cmake for Xcode target versus normal target

I have this simple C ++ 11 code.

#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(); } } 

I am trying to create this code with CMake installed on Xcode and clang ++.

This is 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 and cd build
  • export CC = / usr / bin / clang
  • export CXX = / usr / bin / clang ++
  • cmake ..
  • to do

I could get a binary that works fine.

Xcode 4.5 Goal

  • Same step 1-3
  • cmake .. -G Xcode
  • xcodebuild

The compilation works fine, but when I received an error in the assembly with the Undefined symbol error. I install C ++ 11 with this command in the Cmake 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) 

What could be wrong?

ADDED

From this link, it seems that the target setup is incorrect with CMake's xbuildcode. http://www.executionunit.com/blog/2012/10/27/xcode-std-link-errors/

ADDED

I could fix this problem, but I had to use the same directory where CMakeLists.txt is located.

! [enter image description here] [1]

When I execute CMake .. -G XCode , I got another error.

 === 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]: http://i.stack.imgur.com/xHW5M.png 
+4
source share
1 answer

There were two problems.

Linker Option

As Fraser noted, the problem was with the installation in the linker.

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

Add_subdirectory

I'm not sure why this is necessary, but I needed to use add_subdirectory(src) in the main CMakeLists.txt and make another CMakeLists.txt in this src directory to run cmake in the build directory. Without this, I had to run cmake in the same directory where CMakeLists.txt is located. Cmake generator generator creates a project that cannot build

This is CMakeLists.txt in the src directory.

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

This is CMakeLists.txt in the main directory

 project( XcodeTest ) cmake_minimum_required( VERSION 2.6 ) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++") add_subdirectory(src) 

I got a tip from this youtube video: http://www.youtube.com/watch?v=-uEXVOzd364

enter image description here

+4
source

Source: https://habr.com/ru/post/1487675/


All Articles