Author Topic: C Header Files Inclusion  (Read 8251 times)

Offline Anark10n

  • Single posting newcomer
  • *
  • Posts: 5
C Header Files Inclusion
« 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...

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11770
    • Travis build status
Re: C Header Files Inclusion
« Reply #1 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
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Online stahta01

  • Lives here!
  • ****
  • Posts: 6535
    • My Best Post
Re: C Header Files Inclusion
« Reply #2 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
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Anark10n

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Header Files Inclusion
« Reply #3 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.

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7265
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: C Header Files Inclusion
« Reply #4 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 .

Offline Anark10n

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Header Files Inclusion
« Reply #5 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.

Online stahta01

  • Lives here!
  • ****
  • Posts: 6535
    • My Best Post
Re: C Header Files Inclusion
« Reply #6 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.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Anark10n

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Header Files Inclusion
« Reply #7 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.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11770
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Anark10n

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Header Files Inclusion
« Reply #9 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...