-1

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 を省略して 1 つのステップでコンパイルを行うと、同じことが起こります。私は何日も立ち往生しています!!! その上で、正常にコンパイルできません。

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) 
4

1 に答える 1

1

これは完全に正常です。orの引数の順序は非常に重要ですg++gcc。特に、-lライブラリの引数 ( -lncurses... など) は、常にすべてのオブジェクト ファイルの後に指定する必要があります。thisおよび他の回答を参照し、 GCC のドキュメントを読んでください。

于 2016-05-23T12:44:47.200 に答える