用于显示 strdup 等函数的编译器标志

发布于 2024-10-17 16:26:35 字数 1135 浏览 2 评论 0原文

我已经获得了一些项目的起始代码,我必须在我正在参加的课程中完成。该代码在大学计算机上编译得很好,但是当我尝试在自己的计算机上编译该代码时,由于函数调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2024-10-24 16:26:35

赛斯的回答是正确的。如果它不适合您,还可以将 -std=gnu99 编译器选项传递给 g++(它自动定义所有常见的测试宏)。

请务必在命令行末尾添加所有选项,因为后面的选项会覆盖较新的选项!

以下是 GNU glibc 的测试宏要求(来自手册页):

       Since glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
           _POSIX_C_SOURCE >= 200809L
       Before glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

我不知道为什么需要 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):

       Since glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
           _POSIX_C_SOURCE >= 200809L
       Before glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

I don't know why POSIX >= 200809L is required, since the manpage says it conforms to POSIX.1-2001.

酒几许 2024-10-24 16:26:35

-D_BSD_SOURCE-D_SVID_SOURCE 添加到编译行,您将公开 strdup()

Add -D_BSD_SOURCE or -D_SVID_SOURCE to your compile line and you will expose strdup()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文