apue例子编译的问题

zfrog 2014-07-20 12:36:47
按照网上的方法编译出了libapue.a,但还是出现链接错误
$ gcc fig1.3.cpp -lapue
/tmp/ccswK9BA.o: In function `main':
fig1.3.cpp:(.text+0x20): undefined reference to `err_quit(char const*, ...)'
fig1.3.cpp:(.text+0x60): undefined reference to `err_sys(char const*, ...)'
collect2: error: ld returned 1 exit status

对libapue.a 使用 nm 查看结果,里面应该有这两个函数的哇,请问是什么原因导致链接错误的,怎么解决
error.o:
U abort
0000000000000413 t err_doit
0000000000000209 T err_dump
000000000000015a T err_exit
00000000000002b5 T err_msg
U __errno_location
0000000000000360 T err_quit
0000000000000000 T err_ret
00000000000000a9 T err_sys

...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 colddown 的回复:
[quote=引用 2 楼 micropentium6 的回复:] [quote=引用 1 楼 colddown 的回复:] 因为你的文件后缀是cpp,gcc当作C++文件并自动调用g++去编译,由于C++ name mangle 机制,函数名被修改了。你可以用先把cpp文件编译成.o文件(用-c),再用nm看看就能发现函数名被修改了。具体可以google C++ mangle。 简单的解决方法就是把文件名从.cpp改成.c。 另一种方法就是用extern C防止mangle。
I haven't write make file for C++ using gcc for a while. I am not sure if gcc could decide using g++ by checking the suffix of the file. Also, does APUE offer the makefile for compiling libapue.a? If so, there won't be any "name mangling" in there. nm has proved that. I think you should try to add -L/path2yourlib first. And I don't see -I path for include file as well. don't know why compiler didn't complain at the first place... [/quote] 我试了一下,确实调用的是g++。
$ cat ./main.cpp
int foo();

int main()
{
    foo();
}
$ gcc -c main.cpp $ nm main.o U _Z3foov <- mangled 0000000000000000 T main 下面的信息提示g++被使用。 $ gcc main.cpp -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /build/gcc/src/gcc-4.8-20130725/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-install-libiberty --disable-multilib --disable-libssp --disable-werror --enable-checking=release Thread model: posix gcc version 4.8.1 20130725 (prerelease) (GCC) COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/cc1plus -quiet -v -D_GNU_SOURCE main.cpp -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase main -version -o /tmp/cc1VXGmG.s GNU C++ (GCC) version 4.8.1 20130725 (prerelease) (x86_64-unknown-linux-gnu) compiled by GNU C version 4.8.1 20130725 (prerelease), GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1[/quote] Thanks! You are right on the issue here. just a side note here that using gcc (4.4.7) for cpp will have problem on linking process, certain c++ libraries will not be included. For my case libstdc++ was missing. You will have to explicitly append it to the make file. So, calling g++ directly is the best choice. Anyway, this is not the topic of this post...
  • 打赏
  • 举报
回复
Thanks! You are right on the issue here. just a side note here that using gcc (4.4.7) for cpp will have problem on linking process, certain c++ libraries will not be included. For my case libstdc++ was missing. You will have to explicitly append it to the make file. So, calling g++ directly is the best choice. Anyway, this is not the topic of this post...
  • 打赏
  • 举报
回复
引用 1 楼 colddown 的回复:
因为你的文件后缀是cpp,gcc当作C++文件并自动调用g++去编译,由于C++ name mangle 机制,函数名被修改了。你可以用先把cpp文件编译成.o文件(用-c),再用nm看看就能发现函数名被修改了。具体可以google C++ mangle。 简单的解决方法就是把文件名从.cpp改成.c。 另一种方法就是用extern C防止mangle。
I haven't write make file for C++ using gcc for a while. I am not sure if gcc could decide using g++ by checking the suffix of the file. Also, does APUE offer the makefile for compiling libapue.a? If so, there won't be any "name mangling" in there. nm has proved that. I think you should try to add -L/path2yourlib first. And I don't see -I path for include file as well. don't know why compiler didn't complain at the first place...
colddown 2014-07-20
  • 打赏
  • 举报
回复
因为你的文件后缀是cpp,gcc当作C++文件并自动调用g++去编译,由于C++ name mangle 机制,函数名被修改了。你可以用先把cpp文件编译成.o文件(用-c),再用nm看看就能发现函数名被修改了。具体可以google C++ mangle。 简单的解决方法就是把文件名从.cpp改成.c。 另一种方法就是用extern C防止mangle。
colddown 2014-07-20
  • 打赏
  • 举报
回复
引用 2 楼 micropentium6 的回复:
[quote=引用 1 楼 colddown 的回复:] 因为你的文件后缀是cpp,gcc当作C++文件并自动调用g++去编译,由于C++ name mangle 机制,函数名被修改了。你可以用先把cpp文件编译成.o文件(用-c),再用nm看看就能发现函数名被修改了。具体可以google C++ mangle。 简单的解决方法就是把文件名从.cpp改成.c。 另一种方法就是用extern C防止mangle。
I haven't write make file for C++ using gcc for a while. I am not sure if gcc could decide using g++ by checking the suffix of the file. Also, does APUE offer the makefile for compiling libapue.a? If so, there won't be any "name mangling" in there. nm has proved that. I think you should try to add -L/path2yourlib first. And I don't see -I path for include file as well. don't know why compiler didn't complain at the first place... [/quote] 我试了一下,确实调用的是g++。
$ cat ./main.cpp
int foo();

int main()
{
    foo();
}
$ gcc -c main.cpp $ nm main.o U _Z3foov <- mangled 0000000000000000 T main 下面的信息提示g++被使用。 $ gcc main.cpp -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /build/gcc/src/gcc-4.8-20130725/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-install-libiberty --disable-multilib --disable-libssp --disable-werror --enable-checking=release Thread model: posix gcc version 4.8.1 20130725 (prerelease) (GCC) COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/cc1plus -quiet -v -D_GNU_SOURCE main.cpp -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase main -version -o /tmp/cc1VXGmG.s GNU C++ (GCC) version 4.8.1 20130725 (prerelease) (x86_64-unknown-linux-gnu) compiled by GNU C version 4.8.1 20130725 (prerelease), GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧