Undefined Reference

2006-05-28
2012-10-25
  • Phuc H. Dao

    Phuc H. Dao - 2006-05-28

    I installed the CUnit 2.1-0. I ran the example from
    the CUnit example page that test the fprintf and fread.
    When I compile it using the following Makefile, I get
    many errors. Please take a look below and let me know
    what am missing. The online example is on this site, http://cunit.sourceforge.net/example.html .

    Makefile:

    rule = -pedantic -Wall -ansi -O2

    OnlineExample: clean OnlineExample.o
    gcc $(rule) -o OE OnlineExample.o

    OnlineExample.o: OnlineExample.c
    gcc -c OnlineExample.c

    clean:
    rm -f core. .o

    I get these errors when I compile:

    [phuchdao@HoangHao LATA]$ make OnlineExample
    rm -f core. .o
    gcc -c OnlineExample.c
    gcc -pedantic -Wall -ansi -O2 -o OE OnlineExample.o
    OnlineExample.o(.text+0xca): In function testFPRINTF': : undefined reference toCU_assertImplementation'
    OnlineExample.o(.text+0x10a): In function testFPRINTF': : undefined reference toCU_assertImplementation'
    OnlineExample.o(.text+0x14d): In function testFPRINTF': : undefined reference toCU_assertImplementation'
    OnlineExample.o(.text+0x1b6): In function testFREAD': : undefined reference toCU_assertImplementation'
    OnlineExample.o(.text+0x1f5): In function testFREAD': : undefined reference toCU_assertImplementation'
    OnlineExample.o(.text+0x222): In function main': : undefined reference toCU_initialize_registry'
    OnlineExample.o(.text+0x22b): In function main': : undefined reference toCU_get_error'
    OnlineExample.o(.text+0x24a): In function main': : undefined reference toCU_add_suite'
    OnlineExample.o(.text+0x25b): In function main': : undefined reference toCU_cleanup_registry'
    OnlineExample.o(.text+0x260): In function main': : undefined reference toCU_get_error'
    OnlineExample.o(.text+0x27a): In function main': : undefined reference toCU_add_test'
    OnlineExample.o(.text+0x296): In function main': : undefined reference toCU_add_test'
    OnlineExample.o(.text+0x2a2): In function main': : undefined reference toCU_cleanup_registry'
    OnlineExample.o(.text+0x2a7): In function main': : undefined reference toCU_get_error'
    OnlineExample.o(.text+0x2b6): In function main': : undefined reference toCU_basic_set_mode'
    OnlineExample.o(.text+0x2be): In function main': : undefined reference toCU_basic_run_tests'
    OnlineExample.o(.text+0x2c3): In function main': : undefined reference toCU_cleanup_registry'
    OnlineExample.o(.text+0x2c8): In function main': : undefined reference toCU_get_error'
    collect2: ld returned 1 exit status
    make: *** [OnlineExample] Error 1

    I'm stuck at this stage. Please let me know as soon as you can.

    Thanks,

    Phuc H. Dao

     
    • Harrison B. Caudill

      Yeah, that example code is broken. Take a look at the cunit source code itself:

      kungfoo@artur ~/tmp $ tar xzf cunit-2.0-1.tar.gz
      kungfoo@artur ~/tmp/CUnit-2.0-1 $ cd CUnit-2.0-1
      kungfoo@artur ~/tmp/CUnit-2.0-1 $ autoreconf
      kungfoo@artur ~/tmp/CUnit-2.0-1 $ ./configure
      kungfoo@artur ~/tmp/CUnit-2.0-1 $ make
      kungfoo@artur ~/tmp/CUnit-2.0-1 $ cd Examples/BasicTest
      kungfoo@artur ~/tmp/CUnit-2.0-1/Examples/BasicTest $ ./BasicTest

       
      • Harrison B. Caudill

        ok, so, I should amend my previous post. The problem, apparently, is that you have to place the -lcunit at the end of the compile command:

        kungfoo@artur ~/tmp $ cc -lcunit -o cunit_example cunit_example.c
        /tmp/cc2LVLaR.o: In function testFPRINTF': cunit_example.c:(.text+0xd6): undefined reference toCU_assertImplementation'
        cunit_example.c:(.text+0x124): undefined reference to `CU_assertImplementation'
        ...
        kungfoo@artur ~/tmp $ cc -lcunit -o cunit_example cunit_example.c -lcunit
        kungfoo@artur ~/tmp $

        I'm told that there are some cases where you have to have the -l... before the list of sources/objects, and, in most cases, it's required after the list. It seems to work if you use both, so I suggest adopting a policy of placing the -l... both before and after the list of sources/objects in future Makefiles.

        I really don't see any logical reason for this to be the case, but it is. Apparently, ld treats libraries differently and it's considered standard practice to put your libraries at the end of the link command.

        I'm not sure what the exact code path is that causes the problem, but when I started seeing the undefined reference, I first used strings, and found the function name in /usr/lib/libcunit.a I then used nm, and saw that the library contains undefined symbols for it. Linking is still a bit of a magical black-box to me, so I can't really explain all these things. If anyone can shed some expertise on the situation, I'd be quite appreciative.

        HTH,
        Harrison

         
    • skiloup

      skiloup - 2006-06-29

      I'm having the same difficulty. Let me know if you figure it out. I'll do the same.

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks