Thread: linker error - i must be doing something stupid

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    linker error - i must be doing something stupid

    OK, I'm segregating my code to different files for clarity but seem to be missing something at link time. I use Code:Blocks on Win2k.

    main.c
    Code:
    ...
    #include "mysqlfunc.h"
    ...
          mysqlconn = connect_to_mysql(mysqlconn);
    ...
    mysqlfunc.h
    Code:
    #ifndef MYSQLFUNC_H
    #define MYSQLFUNC_H
    ...
    MYSQL *connect_to_mysql(MYSQL *);
    ...
    #endif
    mysqlfunc.c
    Code:
    MYSQL *connect_to_mysql(MYSQL *mysqlconn)
    {
    ...
    }
    Error message:
    Code:
    main.c     undefined reference to connect_to_mysql(st_mysql*)
    main.c, mysqlfunc.c and mysqlfunc.h are all included in my project.

    What am I missing ? All files compile fine, they just aren't linking properly.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you including mysqlfunc.h inside mysqlfunc.c? Also, are you compiling both .c files?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    yes to both questions, it's really bugging me now.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you sure what you typed here is entirely accurate? Did you accidentally mispell the function name in any of the files?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    I know, baffles me too. No spelling mistakes as I always use copy/paste when I do my prototypes (straight from the function declaration itself), and the same when I am writing the code to call the function (made that mistake too many times in the past).

    The code I posted is direct copy/paste from Code::Blocks. I edited out the full content of the functions/prototypes as I didn't feel they were relevant.

    Could this be something I am missing in my project setup ? some dependancy I am supposed to set ? I haven't written multi-source code using Code::Blocks before.

    I have another function in another file that gives me the same linker error, but I figured if I could find out what I am doing wrong with this the same would apply.

    I have noticed that the compiler variable for main.c is CPP and the compiler variable for mysqlfunc.c is CC, I am not sure how that all works though, and IF I should be changing it.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I noticed the struct was typedef'ed or something. I figured you were compiling everything in C++ since C++ doesn't need the typedef and the typedef provided changed the struct type to MYSQL.

    Anyway, compile them all with either C or C++.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Undefined references usually come from the linker, not the compiler.
    Did you add the libraries, did you get the library names right, and the correct library search paths.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Thank You, Thank You, Thank You.

    I changed the main.c compiler variable to CC and it compiled with no more linker errors. So, I can assume CC means C and CPP is C++, duh. I guess the default was CPP when I first created main.c.

    I love this forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2009, 12:56 PM
  2. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  3. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 01:25 AM
  4. I am gonna kill myself...linker error !!!
    By roalme00 in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2007, 11:34 AM
  5. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM