Find the answer to your Linux question:
Results 1 to 10 of 10
Hi! I'm trying to compile a very simple code which uses functions defined in the Linux PCI Utilities library, but the linker stops with "undefined reference to" errors and im ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4

    Can't solve gcc linking error, please help.


    Hi!

    I'm trying to compile a very simple code which uses functions defined in the Linux PCI Utilities library, but the linker stops with "undefined reference to" errors and im running out of ideas. I'm on ubuntu 14.04 LTS (64-bit) and i've installed the libpci-dev (1:3.2.1-1ubuntu5) package too.

    Here is my test.c:
    Code:
    #include <pci/pci.h>
    
    int main(int argc, char *argv[]){
    
    	struct pci_access *pci = pci_alloc();
    	pci_cleanup(pci);
    	return 0;
    }
    And the compiler output:
    Code:
    /home/dark/Temp/libpci-test# make
    gcc -O3 -Wall -c test.c
    gcc -O3 -Wall -lpci -o test test.o
    test.o: In function `main':
    test.c:(.text.startup+0x5): undefined reference to `pci_alloc'
    test.c:(.text.startup+0xd): undefined reference to `pci_cleanup'
    collect2: error: ld returned 1 exit status
    make: *** [test] Error 1
    But these functions seem to be there..
    Code:
    /home/dark/Temp/libpci-test# nm -D /usr/lib/x86_64-linux-gnu/libpci.so
                     U access
                     U close
                     U closedir
                     U __ctype_b_loc
                     w __cxa_finalize
                     U __errno_location
                     U exit
                     U fclose
                     U ferror
                     U fflush
                     U fgets
                     U fopen
                     U __fprintf_chk
                     U fputc
                     U free
                     U fwrite
                     U gethostname
                     U getpid
                     U getpwuid
                     U getuid
                     w __gmon_start__
                     U gzclose
                     U gzeof
                     U gzerror
                     U gzgets
                     U gzopen
                     U __h_errno_location
                     U iopl
                     U __isoc99_sscanf
                     w _ITM_deregisterTMCloneTable
                     w _ITM_registerTMCloneTable
                     w _Jv_RegisterClasses
    0000000000000000 A LIBPCI_3.0
    0000000000000000 A LIBPCI_3.1
    0000000000000000 A LIBPCI_3.2
                     U malloc
                     U memcmp
                     U memcpy
                     U __memcpy_chk
                     U memset
                     U open
                     U __open_2
                     U opendir
    00000000000027e0 T pci_alloc
    0000000000002a20 T pci_cleanup
    0000000000002f80 T pci_fill_info
    0000000000002f80 T pci_fill_info
    0000000000002f80 T pci_fill_info
    0000000000004c60 T pci_filter_init
    0000000000005040 T pci_filter_match
    0000000000004f20 T pci_filter_parse_id
    0000000000004c90 T pci_filter_parse_slot
    0000000000006950 T pci_find_cap
    0000000000002bf0 T pci_free_dev
    0000000000005980 T pci_free_name_list
    0000000000002bb0 T pci_get_dev
    00000000000027b0 T pci_get_method_name
    00000000000066d0 T pci_get_param
    00000000000063b0 T pci_id_cache_flush
    0000000000002890 T pci_init
    00000000000059d0 T pci_load_name_list
    0000000000002760 T pci_lookup_method
    00000000000043c0 T pci_lookup_name
    0000000000002db0 T pci_read_block
    0000000000002c30 T pci_read_byte
    0000000000002d20 T pci_read_long
    0000000000002dc0 T pci_read_vpd
    0000000000002c90 T pci_read_word
    0000000000002a90 T pci_scan_bus
    00000000000059a0 T pci_set_name_list_path
    0000000000006820 T pci_set_param
    0000000000002fe0 T pci_setup_cache
    0000000000006880 T pci_walk_params
    0000000000002f20 T pci_write_block
    0000000000002de0 T pci_write_byte
    0000000000002ea0 T pci_write_long
    0000000000002e20 T pci_write_word
                     U pread
                     U pwrite
                     U read
                     U readdir
                     U rename
                     U __res_init
                     U __res_query
                     U snprintf
                     U __snprintf_chk
                     U __sprintf_chk
                     U sscanf
                     U __stack_chk_fail
                     U stderr
                     U stdout
                     U strchr
                     U strcmp
                     U __strdup
                     U strerror
                     U strlen
                     U strrchr
                     U strtol
                     U unlink
                     U __vfprintf_chk
                     U zError
    What am I doing wrong?

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    362
    Did you do
    Code:
    ldconfig
    as root after you installed
    /usr/lib/x86_64-linux-gnu/libpci.so
    ?

  3. #3
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4
    Quote Originally Posted by Miven View Post
    Did you do
    Code:
    ldconfig
    as root after you installed
    /usr/lib/x86_64-linux-gnu/libpci.so
    ?
    Thank you, forgot to do that. But still getting the same error.

  4. #4
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    888
    Did you make the makefile by hand? Something might not have been configured correctly...

    check out: build - Bash environment variable to include path of c libraries - Super User

  5. #5
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    166
    Can you post a copy of the Makefile you're using? And if you're not using a Makefile, well that's the problem. The loader won't search every library on the system to resolve the external references in your code. You need to tell it which ones you want to use.

  6. #6
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4
    This is the Makefile im using

    Code:
    CC=gcc
    CFLAGS=-O3 -Wall
    LDFLAGS=-lpci
    
    test: test.o
    	${CC} ${CFLAGS} ${LDFLAGS} -o $@ $^
    
    test.o: test.c
    	${CC} ${CFLAGS} -c $<
    
    .PHONY: clean
    clean:
    	rm -f test *.o

  7. #7
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    166
    Have you tried installing package "libpci-dev"? If not, it will create "/usr/lib/x86_64-linux-gnu/libpci.a" which might resolve the problem.

    I'm wondering if you just have the run-time package installed, which gives you the ".so" files but not the ".a" libraries to link against?

    --- edit ---

    Nevermind the previous comment...

    This was bugging me to so started up a Virtualbox VM and installed the packages, then tried to compile the code. Installing the dev package doesn't help.

    What DOES work is this horrible command:

    Code:
    gcc -O3 -Wall -o test test.o /usr/lib/i386-linux-gnu/libpci.so
    Which is revolting... But it does prove that it's a problem getting the loader/linker to use the installed libraries.

  8. #8
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    166
    Got it... You just need to reorder the rule to create the executable in the Makefile. The library reference has to be listed after the ".o" file, since the linker/loader processes the arguments in order. So change to this:

    Code:
    test: test.o
    	${CC} ${CFLAGS} -o $@ $^ ${LDFLAGS}
    and it should work. It does on my Ubuntu system. Also, FWIW, I always uses parens around my Makefile variables, so I never new things like "${CC}" worked...

  9. #9
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4
    Quote Originally Posted by cnamejj View Post
    Got it... You just need to reorder the rule to create the executable in the Makefile. The library reference has to be listed after the ".o" file, since the linker/loader processes the arguments in order. So change to this:

    Code:
    test: test.o
    	${CC} ${CFLAGS} -o $@ $^ ${LDFLAGS}
    and it should work. It does on my Ubuntu system. Also, FWIW, I always uses parens around my Makefile variables, so I never new things like "${CC}" worked...
    Thank you very much! This small change did the trick.

  10. #10
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    362
    Funny thing: I just copied OP's test.c and Makefile, and I'm getting the exact results he got:
    Code:
    gcc -O3 -Wall -lpci -o test test.o
    test.o(.text+0xd): In function `main':
    : undefined reference to `pci_alloc'
    test.o(.text+0x15): In function `main':
    : undefined reference to `pci_cleanup'
    collect2: ld returned 1 exit status
    make: *** [test] Error 1
    libpci is a part of pciutils for me, but I have only /usr/lib/libpci.a. No .so files at all. Odd.

    But, I did get it to work with this Makefile:
    Code:
    CC=gcc
    CFLAGS=-O3 -Wall
    LDFLAGS= -L/usr/lib -lpci
    LIBS=/usr/lib/libpci.a
    
    test: test.o
                    ${CC} ${CFLAGS} ${LDFLAGS} $^ ${LIBS} -o $@
    
    test.o: test.c
                    ${CC} ${CFLAGS} -c $<
    
    .PHONY: clean
            clean:
                    rm -f test *.o
    Peace and Cheer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •