g ++对象文件参数位置很重要并且给出了无法正常编译的不同结果

时间:2016-05-23 09:25:47

标签: c++ g++ linux-x32-abi

为main.cpp提供一个简单的测试文件(如果不重要),我按如下方式编译和创建目标文件:

g++ -c -mx32 -o main.cpp.o -L/usr/libx32/ -lncurses main.cpp 

创建目标文件。之后,我删除了使用后续命令创建和测试可执行文件:

rm test_ncurses
g++ -o test_ncurses main.cpp.o -L. -lncurses -mx32
./test_ncurses 

但是当我这样做时:

g++ -o test_ncurses -L/usr/libx32/ -lncurses -mx32 main.cpp.o

输出:

main.cpp.o: In function `main':
/xxx/test_ncurses/main.cpp:12: undefined reference to `initscr'
/xxx/test_ncurses/main.cpp:13: undefined reference to `cbreak'
/xxx/test_ncurses/main.cpp:15: undefined reference to `stdscr'
/xxx/test_ncurses/main.cpp:15: undefined reference to `keypad'
/xxx/test_ncurses/main.cpp:19: undefined reference to `LINES'
/xxx/test_ncurses/main.cpp:20: undefined reference to `COLS'
/xxx/test_ncurses/main.cpp:21: undefined reference to `printw'
/xxx/test_ncurses/main.cpp:22: undefined reference to `refresh'
/xxx/test_ncurses/main.cpp:25: undefined reference to `stdscr'
/xxx/test_ncurses/main.cpp:25: undefined reference to `wgetch'
/xxx/test_ncurses/main.cpp:46: undefined reference to `endwin'
main.cpp.o: In function `create_newwin(int, int, int, int)':
/xxx/test_ncurses/main.cpp:53: undefined reference to `newwin'
/xxx/test_ncurses/main.cpp:54: undefined reference to `box'
/xxx/test_ncurses/main.cpp:57: undefined reference to `wrefresh'
main.cpp.o: In function `destroy_win(_win_st*)':
/xxx/test_ncurses/main.cpp:68: undefined reference to `wborder'
/xxx/test_ncurses/main.cpp:80: undefined reference to `wrefresh'
/xxx/test_ncurses/main.cpp:81: undefined reference to `delwin'
collect2: error: ld returned 1 exit status

当我在第一个命令中省略-c的一步编译时,会发生同样的情况。 我被困了DAYS !!!在那,并且无法正常编译。

ld和g ++的版本是:

GNU ld (GNU Binutils for Ubuntu) 2.26
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-14ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --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 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) 

1 个答案:

答案 0 :(得分:1)

这是完全正常的。 g++gcc的参数顺序与批次相关。特别是任何-l库参数(例如-lncurses ...)应始终在所有目标文件之后。请参阅this和其他答案,并阅读documentation of GCC