title: C++ program gets undefined reference to a dynamic C library during linking
body: <p>I've created a dynamic networking library in C. When I try to link it in my C++ program I get an undefined reference the first time I call one of my functions in the library. What I can't understand is that I created a little test program in C that uses the library and it links just fine.</p>
  
  <p>Could someone help with my problem? Here are the steps I've taken to create my library and link it.</p>
  
  <p>Library creation:</p>
  
  <ul>
  <li>Code my library</li>
  <li>Compile with the line: gcc -c -fPIC -g -Wall ./libnet.c</li>
  <li>Link my library object file and create the shared object file with the line: gcc -shared -o libnet.so -g -Wall ./libnet.o</li>
  </ul>
  
  <p>Move my library to the appropriate folders and set LD_LIBRARY_PATH:</p>
  
  <ul>
  <li>I copy my library to two directories. One directory is the test C program that links properly and one is the C++ program that doesn't link properly.</li>
  <li>I set the environment variable LD_LIBRARY_PATH to both the directories that use the library</li>
  </ul>
  
  <p>Compile and link my program:</p>
  
  <ul>
  <li>Code my program</li>
  <li>Compile all my .cpp files with the line: g++ -c -g -Wall ./</li>
  <li>Link together all my object files and the shared library to create my program with the line: g++ -o  -g -Wall -L./ -lnet  </li>
  </ul>
  
  <p>Here is the make file that I use. Maybe I have something wrong in here?</p>
  
  <blockquote>
    <p>PPFLAGS =    -g -Wall</p>
    
    <p>TARGET =    msgfrwdserver</p>
    
    <p>OBJS =     msgfrwdserver.o
    msgfrwdhelper.o msgfrwd.o climsgfrwd.o</p>
    
    <p>LIBS =      libnet.so</p>
    
    <p>CPPLP =
        -L/usr/home/chris/development/legends/servers/monitor
    -L/usr/home/chris/development/legends/servers/msgfrwd</p>
    
    <p>CPPFILES =   ./msgfrwdserver.cpp
    ./msgfrwdhelper.cpp
    ./classes/msgfrwd.cpp
    ./classes/climsgfrwd.cpp</p>
    
    <p>CPPIP =     -I./classes
    -I/usr/home/chris/development/legends/libnet</p>
    
    <p>all: ${OBJS} ${TARGET}</p>
    
    <p>${TARGET}: ${OBJS} ${LIBS}  </p>
    
    <p>g++ -o
    ${TARGET} ${PPFLAGS} -L./ -lnet
    ${CPPIP} ${OBJS}</p>
    
    <p>msgfrwdserver.o: ./msgfrwdserver.cpp</p>
    
    <p>g++ -c ${PPFLAGS} ${CPPIP}
    ./msgfrwdserver.cpp   </p>
    
    <p>msgfrwdhelper.o:
    ./msgfrwdhelper.cpp  </p>
    
    <p>g++ -c ${PPFLAGS}
    ./msgfrwdhelper.cpp   </p>
    
    <p>msgfrwd.o:
    ./classes/msgfrwd.cpp  </p>
    
    <p>g++ -c
    ${PPFLAGS} ./classes/msgfrwd.cpp  </p>
    
    <p>climsgfrwd.o: ./classes/climsgfrwd.cpp</p>
    
    <p>g++ -c ${PPFLAGS} ${CPPIP}
    ./classes/climsgfrwd.cpp   </p>
    
    <p>clean:  rm
    -rf ${TARGET} *.o *~ <em>.core ./classes/</em>~</p>
  </blockquote>
  
  <p>I really have no idea what the problem could be. Any help would be much appreciated. Do I have to do something differently to get a C library to work with a C++ program?</p>
  
  <p>Sorry that the make file is messy. I didn't exactly translate well when I tried to block quote it or say it was a code sample.</p>