用于显示 strdup 等函数的编译器标志
我已经获得了一些项目的起始代码,我必须在我正在参加的课程中完成。该代码在大学计算机上编译得很好,但是当我尝试在自己的计算机上编译该代码时,由于函数调用 strdup 而出现错误。据我所知,这是因为 strdup 不是 ISO c99 函数(https:// bugzilla.redhat.com/show_bug.cgi?id=130815)。我应该如何编译代码?我想我只需要添加一些额外的编译器标志,但我不确定是哪些。如果您需要我运行 g++ -v
的信息,以下是输出:
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
I've been given some starter code for a project I have to complete in a class I'm taking. The code compiles fine on the university computers however when I try to compile the code on my own computer I get errors due the function call strdup. From what I can gather this is caused because strdup is not a ISO c99 function (https://bugzilla.redhat.com/show_bug.cgi?id=130815). How should I go about getting the code to compile? I'd imagine I just need to throw in some additional compiler flags but I'm not sure which ones. In case you need the info I ran g++ -v
, here is the output:
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
赛斯的回答是正确的。如果它不适合您,还可以将 -std=gnu99 编译器选项传递给 g++(它自动定义所有常见的测试宏)。
请务必在命令行末尾添加所有选项,因为后面的选项会覆盖较新的选项!
以下是 GNU glibc 的测试宏要求(来自手册页):
我不知道为什么需要 POSIX >= 200809L,因为手册页说它符合 POSIX.1-2001。
The answer of Seth is correct. If it doesn't work for you, there is also the possibility to pass the -std=gnu99 compiler option to g++ (it automatically defines all common test macros).
Be sure to add all options at the end of the command line, because later options overwrite newer ones!
Here are the test macro requirements for GNU glibc (from manpage):
I don't know why POSIX >= 200809L is required, since the manpage says it conforms to POSIX.1-2001.
将
-D_BSD_SOURCE
或-D_SVID_SOURCE
添加到编译行,您将公开 strdup()Add
-D_BSD_SOURCE
or-D_SVID_SOURCE
to your compile line and you will expose strdup()