Thread: Another "Undefined Reference to..."

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Another "Undefined Reference to..."

    I cannot for the life of my figure this out, probably have been staring at it too long. I have been getting this error
    Code:
    main.c:(.text+0x90): undefined reference to `lreceive'
    Here is what I typically use to compile it:
    Code:
    gcc -o a main.c linsock.c -Wall -pthread -pedantic
    I also tried compiling both of them into separate object files and then linking them, but to no avail...
    What I am doing is creating a thread, but the function the thread uses is in another source file. Would that make a difference?

    main.c
    Code:
    pthread_create(&(info.tid), NULL, lreceive, (void *)&info);
    linsock.c
    Code:
    void *lreceive (void *params)
    {
    	struct linfo *info = (struct *linfo)params;
    	int result = 1;
    	char *buffer = malloc(2048 * sizeof(char));
    
    	while (result)
    	{
    		result = recv(info->fsock, buffer, 2048, 0);
    		if (result)
    		{
    			printf("%d bytes received\n");
    		}
    		else
    			break;
    	}
    
    	return NULL;
    }
    Any words of wisdom?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your main.c needs to include the header where the function is declared.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    It does, sorry for not mentioning it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by carrotcake1029 View Post
    It does, sorry for not mentioning it.
    Then you don't get that error. See for instance:
    main.c
    Code:
    include "linsock.h"
    
    int main() {
        void *(*f)(void*);
        f = lreceive;
        return 0;
    }
    and linsock.c
    Code:
    #include <stdio.h>
    
    void *lreceive(void *params) {
        printf("Hello from linsock!\n");
        return params;
    }
    (with linsock.h being what you expect) which gives the following errors/warnings:
    Code:
    $ gcc -o a main.c linsock.c -Wall -pedantic
    $

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I tried that and it compiled for me, but mine still doesn't...

    I will post some trimmed down files for you, because it sounds like I am overlooking something here:

    Sorry for the long post.

    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "linsock.h"
    
    int main (void)
    {
    	struct linfo info;
    	memset(&info, 0, sizeof(struct linfo));
    	
    	info.host = "google.com";
    	info.port = 80;
    	info.timeout = 5;
    
    	printf("Attempting to connect to %s on port %d\n", info.host, info.port);
    	lconnect(&info);
    	if (info.fsock)
    	{
    		printf("Connected\n");
    		pthread_create(&(info.tid), NULL, lreceive, (void *)&info);
    		pthread_join(info.tid, NULL);
    	}
    	else
    	{
    		printf("%s\n", strerror(errno));
    	}
    
    	close(info.fsock);
    	return 0;
    }
    linsock.c
    Code:
    #include "linsock.h"
    
    void lconnect (struct linfo *info)
    {
        /*
            Yadda Yadda Yadda
        */
    }
    
    void *lreceive (void *params)
    {
    	struct linfo *info = (struct *linfo)params;
    	int result = 1;
    	char *buffer = malloc(2048 * sizeof(char));
    
    	while (result)
    	{
    		result = recv(info->fsock, buffer, 2048, 0);
    		if (result)
    		{
    			printf("%d bytes received\n");
    		}
    		else
    			break;
    	}
    
    	return NULL;
    }
    
    void lresolve (struct linfo *info)
    {
        /*
            Yadda Yadda Yadda
        */
    }
    linsock.h
    Code:
    #ifndef _LINSOCK_H_
    #define _LINSOCK_H_
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #include <fcntl.h>
    
    #include <sys/time.h>
    #include <sys/types.h>
    
    #include <sys/types.h>
    #include <sys/socket.h>
    
    #include <pthread.h>
    
    #include <sys/socket.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    #include <errno.h>
    
    struct linfo
    {
    	int fsock;
    	char *host;
    	char *ip;
    	short port;
    	int timeout;
    	struct sockaddr_in service;
    	pthread_t tid;
    };
    
    void lresolve (struct linfo *info);
    void lconnect (struct linfo *info);
    void *lreceive (void *params);
    
    #endif

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    After I move the * where it belongs (as in "struct linfo*") on line 12, I get:
    Code:
    $ gcc -o a main.c linsock.c -Wall -lpthread -pedantic
    linsock.c: In function ‘lreceive’:
    linsock.c:21: warning: too few arguments for format
    and that's it.

    Edit: And it runs, says it is trying to get to google.com on port 80, and then dies with unknown error 0.

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well crap man, why is my gcc screwing up. I'm using gcc version 4.3.2. I highly doubt the fact that my gcc is about a week out of date makes a difference...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I'm running 4.0 so I doubt they broke it since then. Tell you what: can you call the function lreceive? As in just "lreceive(&info)" and see what happens. I doubt you would have fixed a spelling in copy-paste, but I'm starting to run out of ideas.

    Edit: Oh, and something else that shouldn't make a difference: you can try &lreceive in the lpthread_create call and see if that helps. (I don't see how it could, but hey.)
    Last edited by tabstop; 01-28-2009 at 11:02 PM.

  9. #9
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I cannot. I'm out of ideas too.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you can't call it, then either (a) the header isn't there, somehow, or (b) the function name is mysteriously spelled differently in the header or (c) something else I haven't thought of yet. If you're not getting errors for anything else, then (a) is right out. Edit: And since it's a link error and not a compile error, (b) is right out too.
    Last edited by tabstop; 01-28-2009 at 11:28 PM.

  11. #11
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Yes, currently that is the only error I am getting. I think I agree with part (c).

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Alright, well, what's your command line? Are you using a makefile?

  13. #13
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I've tried both command line and Makefile. I have tried compiling both sources into object files first and then compiling them. Both of them ultimately end up with the same error.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Silly idea: put linsock.c first on the command line before main.c (does ld still need functions found before use? I thought they fixed that).

  15. #15
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Not getting anything new. It could be that I am possibly using a bad compile of gcc. It is doubtful, I know, but I'll compile the newest one tomorrow maybe.

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