簡體   English   中英

C ++ 0x lambdas的未定義符號?

[英]Undefined symbols for C++0x lambdas?

當我遇到一個絆腳石時,我只是在C ++ 0x中尋找一些新東西:

#include <list>
#include <cstdio>
using namespace std;

template <typename T,typename F>
void ForEach (list<T> l, F f) {
    for (typename list<T>::iterator it=l.begin();it!=l.end();++it)
        f(*it);
}

int main() {
    int arr[] = {1,2,3,4,5,6};
    list<int> l (arr,arr+6);
    ForEach(l,[](int x){printf("%d\n",x);});
}

不編譯。 我得到一堆未定義的符號錯誤。 這是make的輸出:

 i386-apple-darwin9-gcc-4.5.0 -std=c++0x -I/usr/local/include -o func main.cpp
Undefined symbols:
  "___cxa_rethrow", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator new(unsigned long)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in ccPxxPwU.o
  "___cxa_begin_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator delete(void*)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::deallocate(std::_List_node<int>*, unsigned long) in ccPxxPwU.o
  "___cxa_end_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "std::__throw_bad_alloc()", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "std::_List_node_base::_M_hook(std::_List_node_base*)", referenced from:
      void std::list<int, std::allocator<int> >::_M_insert<int const&>(std::_List_iterator<int>, int const&&&) in ccPxxPwU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [func] Error 1

為什么這不起作用?

這些都是鏈接錯誤。 你錯過了一個C ++庫嗎? 如果您使用i386-apple-darwin9-g++-4.5.0怎么辦?

PS我無法測試這個,因為我的Mac上有gcc 4.2.1(i686-apple-darwin10-gcc-4.2.1)並得到:

cc1plus: error: unrecognized command line option "-std=c++0x"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM