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

C++ g++;对象文件参数的位置很重要,并且给出了无法正常编译的不同结果,c++,g++,linux-x32-abi,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_nc

有一个简单的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一步编译时,也会发生同样的情况。 我被困了好几天!!!无法正常编译

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) 

这是完全正常的
g++
gcc
的参数顺序非常重要。尤其是任何
-l
库参数(例如
-lncurses
..)都应始终位于所有对象文件之后。请参阅和其他答案并阅读。

我不知道这是否正常,但我知道的是,虽然我以前能够在一次编译中编译(不使用-c)事实上,我没有明显的理由无法做到这一点。根据您的指示,我可以使用g++-std=c++1z-mx32-o test_ncurses main.cpp-L/usr/libx32/-lncurses在源文件开头移动来再次编译。我假设选项排在第一位,参数排在最后,就像我现在在Linux中看到的所有其他命令一样。是学习的时候了。