-1

Having a simple testing file for main.cpp ( not if importance ) I compile and create the object file as follows:

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

The object file is created. Afterwards I delete create and test the executable file with the subsequent command:

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

But when I do:

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

it outputs:

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

The same happens when I do the compilation in one step ommitting the -c in the first command. I am stuck DAYS !!! on that and unable to COMPILE normally.

The version of ld and g++ is:

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) 
George Kourtis
  • 2,381
  • 3
  • 18
  • 28

1 Answers1

1

This is completely normal. The order of arguments to g++ or gcc matters a big lot. In particular any -l library argument (e.g. -lncurses...) should always go after all object files. See this and other answers and read the documentation of GCC.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I don't exactly know if it is normal, what I know is that while I was able to compile before in one pass ( without using -c ) actually I am unable to do it with no apparent reason. – George Kourtis May 23 '16 at 12:47
  • According to your indications I was able to compile again using g++ -std=c++1z -mx32 -o test_ncurses main.cpp -L/usr/libx32/ -lncurses moving at the beginning the source file. I was supposing that options were coming first and arguments last as in all other commands I have seen now in Linux. Time to learn. – George Kourtis May 23 '16 at 15:45