Thread: Undefined reference

  1. #1
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50

    Undefined reference

    I am using Cygwin and trying to compile a program with gcc, but get these "undefined reference" errors for many functions. They all seem to be stdio.h functions, but I am not getting an error saying stdio.h is not found, so maybe its something within that is corrupt? Any help is appreciated!

    Code:
    XXXXXX.o: In function `XXXXXX':
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:470: undefined reference to `fgets'
    XXXXXX.o: In function `main':
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:161: undefined reference to `fseek'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:174: undefined reference to `strtod'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:161: undefined reference to `fseek'
    XXXXXX.o: In function `XXXXXX':
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:577: undefined reference to `memcpy'
    XXXXXX.o: In function `XXXXXX':
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:397: undefined reference to `sscanf'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:400: undefined reference to `__ctype_ptr'
    XXXXXX.o: In function `XXXXXX':
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:491: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:494: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:513: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:514: undefined reference to `sscanf'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:519: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:541: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:532: undefined reference to `sscanf'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:504: undefined reference to `sscanf'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:513: undefined reference to `__ctype_ptr'
    /cygdrive/c/XXXXXX/XXXXXX/GNU/XXXXXX_GNU/XXXXXX/build/../XXXXXX.c:537: undefined reference to `memcpy'

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have glibc somewhere? Somewhere where gcc can find it?

  3. #3
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    I'm using Cygwin, so it has newlib instead of glibc. I can compile other programs but can't seem to figure out why I am getting these errors. Thanks for your input!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you can compile other things but not this, does the makefile that you are using (I'm making a big assumption here) set paths assuming glibc? (I'm seeing a lot of GNU in the error messages.) Now __ctype_ptr is (or should be, since it starts with __) implementation-specific, and I don't know whether newlib uses it or not.

  5. #5
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    I don't think my makefile is assuming that I'm using glibc, it was a template from another program that ran with Cygwin. Should I add a LIBS command or something to declare where to look for stdio.h? Here is my makefile:

    Code:
    bin_PROGRAMS = File1 \
    		   File1.chksum.elf \
    		   File1.y16 
    
    SOURCE_DIR = $(top_srcdir)
    SOURCE_SRC_DIR = $(SOURCE_DIR)
    SOURCE_INCLUDE_DIR = /../include
    SOURCE_COMMON_DIR = $(SOURCE_DIR)/../common
    SOURCE_LDSCRIPT_DIR = $(SOURCE_DIR)
    SOURCE_PROM_DIR = $(SOURCE_DIR)
    SOURCE_DOC_DIR = $(SOURCE_DIR)/doc
    SOURCE_DOC_NOTES_DIR = $(SOURCE_DOC_DIR)/notes
    SOURCE_DOC_EXAMPLES_DIR = $(SOURCE_DOC_DIR)/examples
    
    BUILD_DIR = $(top_builddir)
    BUILD_SRC_DIR = $(BUILD_DIR)
    
    ##Line 23
    
    BUILD_PROM_DIR = $(BUILD_DIR)
    BUILD_DOC_DIR = $(BUILD_DIR)/doc
    
    
    #List of files that are sources to File1.exe
    File1_SOURCES = File1Main.c \
                    File2.c \
                    File3.c \
                    File4.c \
                    File5.c \
                    File6.c \
                    File7.c \
                    $(SOURCE_COMMON_DIR)/File8.c \
                    $(SOURCE_COMMON_DIR)/File8Calls.s \
                    $(SOURCE_COMMON_DIR)/Cksum.s \
                    $(SOURCE_COMMON_DIR)/bits.s 
    
    AM_CFLAGS=-O3 -Wno-main -fomit-frame-pointer -I$(SOURCE_INCLUDE_DIR) \
              -m68000 $(DEBUG) \
              -Wa,-alh="$(BUILD_PROM_DIR)/`echo "$@" | sed -e "s/\.$(OBJEXT)/\.lst/g"`",-L
    AM_CCASFLAGS=$(AM_CFLAGS)
    
    
    #Linker Flags
    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L`m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"`
    File1_LINK=m68k-elf-ld $(AM_LDFLAGS) -o $@ 
    File1_LDADD=-lgcc
    
    M68K_SREC_CHKSUM = @M68K_SREC_CHKSUM@
    
    
    
    
    .PHONY: File1.y16 File1.chksum.elf
    SUFFIXES = .y16 .elf
    
    File1.chksum.elf: File1
    	@ m68k-elf-objcopy \
    		--srec-len=14 \
    		-I elf32-m68k \
    		-O srec \
    		--set-start 0x0000 \
    		--change-section-address GPARMS=0x0000 \
    		--change-section-address APPLINIT=0x000c \
    		--change-section-address APPPROG=0x0100 \
    		--change-section-address .text=0x0100 \
    		--no-change-warnings \
    		--remove-section=APPDATA \
    		--remove-section=.bss \
    		--remove-section=COMMON \
    		$< \
    		$<.srec
    	@ echo -n "File1.y16 : " > File1-checksums.txt
    	@ $(M68K_SREC_CHKSUM) -T word-2s-comp-big -I $<.srec -A 0xFFFE -V 0xFFFF >> File1-checksums.txt
    	@ echo "" >> File1-checksums.txt
    	@ echo "unsigned short __attribute__ ((section (\".checksum\"))) checksum=`$(M68K_SREC_CHKSUM) -T word-2s-comp-big -I $<.srec -R -A 0xFFFE -V 0xFFFF`;" > gm-checksum.c
    	@ echo "Building Application with Checksum"
    	@ $(CC) $(CFLAGS) -c File1-checksum.c -o File1-checksum.o
    	@ $(File1_LINK) $(File1_OBJECTS) File1-checksum.o $(File1_LDADD)
    
    File1.y16: File1.chksum.elf
    	@ m68k-elf-objcopy \
    		--srec-len=14 \
    		--set-start 0x0000 \
    		--change-section-address GPARMS=0x0000 \
    		--change-section-address APPLINIT=0x000c \
    		--change-section-address APPPROG=0x0100 \
    		--change-section-address .text=0x0100 \
    		--change-section-address .checksum=0xFFFE \
    		--no-change-warnings \
    		--remove-section=APPDATA \
    		--remove-section=.bss \
    		--remove-section=COMMON \
    		-I elf32-m68k \
    		-O srec \
    		$< \
    		$@
    	@ m68k-elf-objcopy \
    		--srec-len=14 \
    		--set-start 0x0000 \
    		--change-section-address GPARMS=0x10000 \
    		--change-section-address APPLINIT=0x1000c \
    		--change-section-address APPPROG=0x10100 \
    		--change-section-address .text=0x10100 \
    		--change-section-address .checksum=0x1FFFE \
    		--no-change-warnings \
    		--remove-section=APPDATA \
    		--remove-section=.bss \
    		--remove-section=COMMON \
    		-I elf32-m68k \
    		-O srec \
    		$< \
    		./tests/test.h68

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I don't think my makefile is assuming that I'm using glibc, it was a template from another program that ran with Cygwin. Should I add a LIBS command or something to declare where to look for stdio.h? Here is my makefile:
    It already knows where stdio.h is -- that's not the error you are getting. The problem is you are not linking to an implementation of the standard C library. I don't know much about cygwin, but I would first make sure you can compile and run a simple "Hello World" program.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    Quote Originally Posted by bithub View Post
    It already knows where stdio.h is -- that's not the error you are getting. The problem is you are not linking to an implementation of the standard C library. I don't know much about cygwin, but I would first make sure you can compile and run a simple "Hello World" program.
    Yes that's true. I have run other programs successfully which is why I suspect my makefile has errors

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I see -m68000 there in your flags. Are you trying to use this as a cross-compiler and create an executable for a 68K processor?

  9. #9
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    Quote Originally Posted by tabstop View Post
    I see -m68000 there in your flags. Are you trying to use this as a cross-compiler and create an executable for a 68K processor?

    Yes, sorry I didn't explain that earlier. I have another program that I have done the same thing for and it worked, so I used those files as templates and added on. I don't get these errors when compiling the other program

  10. #10
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    Update: I changed the line

    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L`m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"

    to

    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L../../../../cygwin/opt/m68k/m68k-elf/lib-m68000 --print-libgcc | sed -e "s/libgcc.a//g"

    And I no longer get the reference errors...just other ones.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheEngineer View Post
    Update: I changed the line

    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L`m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"

    to

    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L../../../../cygwin/opt/m68k/m68k-elf/lib-m68000 --print-libgcc | sed -e "s/libgcc.a//g"

    And I no longer get the reference errors...just other ones.
    Really? Why? Are you sure that's what you did? I can't imagine --print-libgcc | sed -e "s/libgcc.a//g" being a very good compiler flag.

  12. #12
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    Quote Originally Posted by tabstop View Post
    Really? Why? Are you sure that's what you did? I can't imagine --print-libgcc | sed -e "s/libgcc.a//g" being a very good compiler flag.
    the --print stuff was already there (was in the file when I borrowed it from another working program and didn't want to mess with it), I just added the path to the m68k library
    Last edited by TheEngineer; 08-11-2009 at 12:53 PM. Reason: spelling error

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So one day someone should show you what ` means. Maybe today's that day:

    When bash sees `, it runs that command somewhere, and substitutes the result back in the command to be run. So this:
    Code:
    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L`m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"`
    causes the command in the backticks, namely
    Code:
    m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"
    to be run, the result of which is pasted back in the AM_LDFLAGS variable after the -L. (Presumably m68k-elf-gcc etc causes the absolute paths of gcclib.a to be printed, because sed is looking for that filename and strips it off, leaving just the path.) This means that gcc never sees --print-libgcc, or |, or sed, which is good, because otherwise it would then try to pipe the result of gcc into sed and heaven only knows what's going to happen then.

  14. #14
    Registered User TheEngineer's Avatar
    Join Date
    Aug 2009
    Posts
    50
    Quote Originally Posted by tabstop View Post
    So one day someone should show you what ` means. Maybe today's that day:

    When bash sees `, it runs that command somewhere, and substitutes the result back in the command to be run. So this:
    Code:
    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L`m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"`
    causes the command in the backticks, namely
    Code:
    m68k-elf-gcc -m68000 --print-libgcc | sed -e "s/libgcc.a//g"
    to be run, the result of which is pasted back in the AM_LDFLAGS variable after the -L. (Presumably m68k-elf-gcc etc causes the absolute paths of gcclib.a to be printed, because sed is looking for that filename and strips it off, leaving just the path.) This means that gcc never sees --print-libgcc, or |, or sed, which is good, because otherwise it would then try to pipe the result of gcc into sed and heaven only knows what's going to happen then.

    Please excuse my ignorance (I did not write these files). I understand now the operation you described. I changed the line to:

    Code:
    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L../../../../cygwin/opt/m68k/m68k-elf/lib -m68000
    When i recompiled, I got the error:

    "m68k-elf-ld: cannot find -lgcc"

    Removing the -lgcc command gives reference errors again

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheEngineer View Post
    Please excuse my ignorance (I did not write these files). I understand now the operation you described. I changed the line to:

    Code:
    AM_LDFLAGS=-M -cref -File1 File1.File1 -T $(SOURCE_LDSCRIPT_DIR)/File1.ld -L../../../../cygwin/opt/m68k/m68k-elf/lib -m68000
    When i recompiled, I got the error:

    "m68k-elf-ld: cannot find -lgcc"

    Removing the -lgcc command gives reference errors again
    So, do you have libgcc.a at ../../../../cygwin/opt/m68k/m68k-elf/lib? And that's a lot of up directories -- maybe try an absolute path instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM