Code::Blocks

User forums => General (but related to Code::Blocks) => Topic started by: Anark10n on April 22, 2010, 01:17:03 pm

Title: C Header Files Inclusion
Post by: Anark10n on April 22, 2010, 01:17:03 pm
Hey there

New to the forum, searched all over for an answer to the problem I'm having, didn't find it. New to using code blocks as well, got a problem with including my header files, probably with linking, not sure, but it keeps giving me the same error message: undefined reference to '_<function_name>'. Any clarity on the problem would be appreciated. Side note: the code compiles without problem in visual studio express.

Thanks in advance to anyone who replies...
Title: Re: C Header Files Inclusion
Post by: oBFusCATed on April 22, 2010, 02:38:25 pm
Read this: http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F

You have a linking problem... probably missing file or library
Title: Re: C Header Files Inclusion
Post by: stahta01 on April 22, 2010, 06:53:46 pm
Side note: the code compiles without problem in visual studio express.

FYI:
Visual Studio can automatically include some libraries and also uses #pragma commands to include libraries.

Tim S
Title: Re: C Header Files Inclusion
Post by: Anark10n on April 27, 2010, 07:28:46 pm
Thanks for the replies, but it is still giving me the same error message. Thought it might just windows only bug so i tried it on a Linux machine seeing as it usually comes with the GCC compiler pre-installed with all the necessary files, and it still gave me the same error message.
Title: Re: C Header Files Inclusion
Post by: jens on April 27, 2010, 08:03:55 pm
The problem can happen, if you mix c++-code with c-code.
If that's the case, you should read this: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html) .
Title: Re: C Header Files Inclusion
Post by: Anark10n on April 28, 2010, 01:40:07 pm
I doubt it's a mix of c and c++, see for yourself:

My header file: Largest.h
int maximum(int x, int y, int z);

My function definitions: LargestDef.c
#include "Largest.h"

int maximum(int x, int y, int z)
{
   int maxNum = x;

   if (maxNum < y)
   {
      maxNum = y;
   }

   if (maxNum < z)
   {
      maxNum = z;
   }

   return maxNum;
}

My main function
#include <stdio.h>
#include "Largest.h"

int main()
{
    int num1;
    int num2;
    int num3;

    printf("Enter three integers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    printf ("Largest: %d", maximum(num1, num2, num3));

    return 0;
}

Not certain whether its appropriate to post code samples here, so apologies if it is not.
Title: Re: C Header Files Inclusion
Post by: stahta01 on April 28, 2010, 01:54:00 pm
I doubt it's a mix of c and c++, see for yourself:

I suggest turning on full compiler logging.
Or, renaming main.cpp to main.c (if it was named wrong).

Tim S.
Title: Re: C Header Files Inclusion
Post by: Anark10n on April 28, 2010, 09:44:03 pm
File containing the main function is also a .c file, sorry about the omission. As for the solutions, no luck; being new to code blocks, i don't exactly know what it is I'm supposed to look for in the compiler logs. Not a surprise though that it compiles fine if i chuck all the code in the same file.
Title: Re: C Header Files Inclusion
Post by: oBFusCATed on April 28, 2010, 09:50:36 pm
Read this: http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F
Title: Re: C Header Files Inclusion
Post by: Anark10n on May 06, 2010, 12:52:25 pm
Thanks for the help, played around with the compiler options (had to look in a lot of places to figure it out), got everything to link properly. Not the most elegant of solutions, but at least it works. Great IDE by the way, hooray for open source...