-4

I recently wanted to test my C project on visual studio (I used visual studio code) but for some reason my other file (file.c) do not recognize function from my main file (main.c). I need to manually add #include in both files. I can compile the code normally using cmd (gcc main.c). If you have any idea what cause this I would be happy to hear.

main.c:

#include <stdio.h>
#include <stdlib.h>

int randNumber = 10;

#include "file.c"

int main ()
{
  int a = 10;
  int b = 2;
  printf ("%i", multiplication (a, b));
  return 0;
}

file.c:

int multiplication (int a, int b)
{
  return a * b * randNumber;
}

This runs normally in vs code but not in visual studio. (I can also compile the code with cmd outside of visual studio with the command (gcc main.c)) The thing that doesn't work is the randNumber, it gets an error saying (identifier "randNumber" is undefined).

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
ErayZaxy
  • 66
  • 2
  • 8
  • 4
    Never include .c files. Read this: https://stackoverflow.com/questions/7109964/creating-your-own-header-file-in-c. Also please don't post pictures of code, post code as text. – Lundin Mar 08 '21 at 11:27
  • 3
    Also, what do you think that `static` in front of a function definition is good for...? Why do you use it? – Lundin Mar 08 '21 at 11:29
  • I don't know I follow a tutorial. – ErayZaxy Mar 08 '21 at 11:42
  • @ErayZaxy you probably misunderstood something in the tutorial. If possible show us that tutorial. – Jabberwocky Mar 08 '21 at 11:54
  • 1
    I notice one of your images shows `#define true 1;` and `#define false 0;`. Putting a semi-colon on the end of those pre-processor directives is a _really_ bad idea. – Ian Abbott Mar 08 '21 at 12:06
  • @ErayZaxy Well it was a rhetorical question. The sole purpose of `static` functions is to block other parts of the program from calling those functions, so called _private encapsulation_. – Lundin Mar 08 '21 at 13:13

1 Answers1

1

You should not include source files (.c) in other source files. software_rendering.c uses types defined in win32_platform.c. For example, the Render_Buffer structure.

You need to put the common types and definitions in a header file (.h) and include that. Keeping the Render_Buffer example: move the Render_Buffer structure definition in a header file and include that header file in both source files.

See Creating your own header file in C for details.

As a side note: please do not post images of your code, instead paste it as text and properly format it as code.

icebp
  • 1,608
  • 1
  • 14
  • 24