giovinazzo17
giovinazzo17

Reputation: 35

How to make c program modular with files.h in Codeblocks?

i hope you really can help me. When i try to divide my c program into files.h i run into this error: undefined reference to value (name of the example function i'm using) in the main.c.

error in main.c

As you can see from the images when i try to compile functions.c it gives me the error: undefined reference to WinMain. Right now i'm using codeblocks but i get the same error on VS Code.

functions.c

I read lot of stuff about making new project with Codeblocks but it doesn't work. Here's my functions.h:

functions.h

I hope someone can help me. Thank you in advance

Upvotes: 0

Views: 620

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84599

You most likely have two issues preventing value() from being found, either:

  1. You have not included functions.c as a file to be linked, or
  2. You have not included functions.h in your include search path.

The easiest way to make sure that functions.c will be linked as part of the project, is to Right-Click on the file and choose Properties.. and confirm it is one of the files to be linked:

enter image description here

To ensure your header has been included as part of the project and in the search path, from the Menu Project -> Properties... and check the Build Target Files (bottom part of dialog), e.g.

enter image description here

With function.c, and your provamodulare.c set to be linked (1) and functions.h as part of the build target files, you can build the project just by choosing build.

Note

If you plan on using the update value of x back in your main file (e.g. in your provamodulare.c), you need to pass a pointer (e.g. pass the address of x) so that the value at that memory address can be updated in value() and the change be visible back in the calling function, e.g.

void value (int *x)
{
    *x += 1;
    printf ("%d\n", *x);
}

and then in the caller, you would do:

    value (&x);

after which you could output the updated value in the caller, e.g.

    printf ("updated value in caller: %d\n", x);

Double check your settings in Code::Blocks as shown above and try again and let me know if you have further problems.

Upvotes: 1

Hiro
Hiro

Reputation: 139

In functions.c you have used printf, however not included its declaration. Add stdio.h header in this file too.

And also see this wiki and user manual working with projects section to troubleshoot the build issues you are facing.

Upvotes: 0

Related Questions