MacPorts GCC 4.7 OS-X Mavericks 10.9 C11中未定义的符号“toupper”

时间:2013-10-27 18:34:48

标签: c++ macos gcc xcode5 macports

EDIT2:

所以这是程序的一个例子:

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
 }

1)clang:

clang++ -std=c++0x -stdlib=libc++ -lc++ main.cc -o main

编译好。

2)g++-mp-4.8 -std=c++11 main.cc -o main给出:

Undefined symbols for architecture x86_64:
  "toupper(int)", referenced from:
      _main in ccWjHauc.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

3)g++-mp-4.8 main.cc -o main编译!

任何想法设置有什么问题?

==========

有人可以帮助理解Gcc / macports / os 10.9中的变化吗?

我曾经在os 10.8中使用过某些第三方库的编译脚本。 最近我更新到新的osx(10.9)和macports的gcc 4.7停止链接。特别是我有:

Undefined symbols for architecture x86_64:
 "isspace(int)", referenced from:

此问题与istype提及here的问题非常类似。 然而,似乎isspace不在libgcc ++。dylib。

任何想法尝试什么?

EDIT1:

确实,4.8修复了isspace的问题,但另一个浮出水面 - toupper

Undefined symbols for architecture x86_64:
  "toupper(int)", referenced from: ...

这是怎么回事?!它与新的Xcode(5.0)有关吗?

3 个答案:

答案 0 :(得分:10)

http://trac.macports.org/ticket/41033中有一个补丁 它解决了我的问题。 您只需要在/usr/include/sys/cdefs.h中修补该文件并替换

#elif defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)

通过

#elif defined(__GNUC__) && defined(__GNUC_STDC_INLINE__) && !defined(__cplusplus)
祝你好运。

答案 1 :(得分:4)

大多数ctype.h项都被声明为内联定义,因此它们会在编译时扩展。在没有-std=c++11的情况下编译时,它会扩展为:

extern inline int 
toupper(int _c) 
{ 
        return (__toupper(_c)); 
} 

使用-std=c++11进行编译时,会扩展为:

extern inline __attribute__((__gnu_inline__)) int 
toupper(int _c) 
{ 
        return (__toupper(_c)); 
}

出于某种原因,g ++然后选择忽略那里提出的非常好的定义。

根据评论on this invalid bug,gcc选择不优化代码并在其中一个链接库中查找定义。

解决方法似乎是至少使用-O1优化进行编译,这样可以避免这个问题,但这对于屁股来说真是太痛苦了。

现在,当我们看一下非C ++ 11和C ++ 11之间#defines的差异时,我们发现我们有一个额外的#define:

$ touch x.cc
$ g++-4.9 -dM -E x.cc | grep STD
#define __STDC_HOSTED__ 1
#define __STDC__ 1
$ g++-4.9 -std=c++11 -dM -E x.cc | grep STD
#define __STDC_HOSTED__ 1
#define __GNUC_STDC_INLINE__ 1
#define __STDC__ 1

并且由于10.9 SDK(usr/include/sys/cdefs.h)中的一段代码,__DARWIN_CTYPE_TOP_inline中的所有cytpe.h变为__header_inline,后者变为extern __inline __attribute__((__gnu_inline__))感谢这一点额外的代码:

#elif defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
# define __header_inline           extern __inline __attribute__((__gnu_inline__))

看起来苹果的标题正在尝试做正确的事情,但他们并未涵盖所有基础。有another issue,提到了类似的错误。

答案 2 :(得分:0)

我已经开始阅读有关特立独行的链接器错误几天了。这个问题似乎与某些交叉链接工具一起出现,这些工具曾经兼容,但更长的是。

您是否尝试过强制所有工具使用一种类型的clang ++或llvm-g ++?