linuxundefined-referencesctp

Why I failed to compile a sctp program in Linux?


First I install sctp on Ubuntu 12.04
sudo apt-get install libsctp-dev lksctp-tools
Then in my .c file,I include :

#include < netinet/in.h >
#include < netinet/sctp.h >
#include < sys/socket.h >
#include < stdlib.h >
#include < unistd.h >

howerver,when I compiled with gcc,the result is:

 undefined reference to `sctp_recvmsg'
 undefined reference to `sctp_get_no_strms'
 undefined reference to `sctp_sendmsg'

What is wrong?


Solution

  • If you really compile with gcc temp.c -o temp then you are not linking any libraries (except the default libc.6.so), and you need some additional argument to gcc ; perhaps try to compile with

     gcc -Wall -g temp.c -lsctp -o temp
    

    Once your program is debugged with the help of the gdb debugger and you consider it to be bug-free, you may ask the compiler to optimize it using

     gcc -Wall -O2 temp.c -lsctp -o temp
    

    The order of program arguments to gcc is important and significant.