Results 1 to 4 of 4

Thread: linking static library issue (collect2: ld returned 1 exit status)

  1. #1
    Join Date
    Dec 2010
    Beans
    7

    linking static library issue (collect2: ld returned 1 exit status)

    Hey there I'm having trouble linking a static lib in my app (C program), I don't understand why this won't work as I am following the instructions from the "Programming Linux Games", Page 29 "Working with Libraries"

    For simplicity's sake I'm trying to link the following source as a static lib to a main.c:

    Code:
    /* myCharLib.c */
    #include "myCharLib.h"
    const char get_char(){
    	return MY_CHAR;
    }
    Code:
    #ifndef MYCHARLIB_H_
    #define MYCHARLIB_H_
    const char MY_CHAR = 'J';
    const char get_char();
    #endif
    Code:
    /* myIntLib.c */
    #include "myIntLib.h"
    const int get_integer()
    {
    	return MY_INT;
    }
    Code:
    #ifndef MYINTLIB_H_
    #define MYINTLIB_H_
    const int MY_INT = 84;
    const int get_integer();
    #endif
    And here is the make file I'm using:
    Code:
    CC=gcc
    CFLAGS=-W -Wall -pedantic
    
    libcMyLib.a: myCharLib.c myIntLib.c
    	$(CC) $(CFLAGS) -c myCharLib.c myIntLib.c
    	ar rcs libcMyLib.a myCharLib.o myIntLib.o
    	ranlib libcMyLib.a
    
    .PHONY: clean
    
    clean:
    	rm *.o
    	rm *.a
    And my main.c file:
    Code:
    #include <stdio.h>
    const int get_int();
    const char get_char();
    int main()
    {
    	get_int();
    	get_char();
    	return 0;
    }
    So my working directory has these files after running make:
    Code:
    libcMyLib.a  main.c  makefile  myCharLib.c  myCharLib.h  myCharLib.o  myIntLib.c  myIntLib.h  myIntLib.o
    Now I try compiling main.c and linking with libMyLib.a and this is what I get:
    Code:
    gcc -static main.c -L. -lcMyLib -o main 
    /tmp/cc0wiQCm.o: In function `main':
    main.c:(.text+0x7): undefined reference to `get_int'
    main.c:(.text+0xc): undefined reference to `get_char'
    collect2: ld returned 1 exit status
    Where exactly am I going wrong here?

    *Edit, yes I am aware of the sticky, but I've also tried following that advice and end up with the same linking problem
    Last edited by _joachim_; May 5th, 2011 at 11:46 PM.

  2. #2
    Join Date
    Mar 2006
    Beans
    837

    Re: linking static library issue (collect2: ld returned 1 exit status)

    I didn't test your code but shouldn't "get_int()" be "get_integer()" in your main?

    (But I doubt that this is your main problem.)

  3. #3
    Join Date
    Dec 2010
    Beans
    7

    Re: linking static library issue (collect2: ld returned 1 exit status)

    Quote Originally Posted by SledgeHammer_999 View Post
    I didn't test your code but shouldn't "get_int()" be "get_integer()" in your main?

    (But I doubt that this is your main problem.)
    Oh ffs, you are right, goddammit I've been up all night wondering and trying to figure out why it wouldn't link, and it was a typo this whole time, fml

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: linking static library issue (collect2: ld returned 1 exit status)

    You have additional issues...

    The first issue is quite obvious... you should not be redeclaring the library's function in your main.c file; what you should do is include the header files that you desire from the library. For example:
    Code:
    #include "myCharLib.h"
    #include "myIntLib.h"
    
    int main(void)
    {
       ...
    }
    The second issue is that you are not declaring the const values in your header file as static. Thus if you follow the suggestion above, then the linker will complain that there are multiple definitions for your const values. To remedy the situation, do something like:
    Code:
    #ifndef MYCHARLIB_H_
    #define MYCHARLIB_H_
    static const char MY_CHAR = 'J';
    const char get_char();
    #endif

Bookmarks

Posting Permissions

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