In my header file file.h I have:
#ifdef FILE_H
extern "C" {
#endif
int size;
int array[3];
void saveToFile();
void loadFromFile();
#ifdef FILE_H
}
#endif
Your multi-inclusion guards have the wrong sense and scope. Also, if your intention is for this header to be usable in both C and C++, then the extern "C" declaration is incorrectly protected. Perhaps you want this:
#ifndef FILE_H
#define FILE_H
#ifdef __cplusplus
extern "C" {
#endif
int size;
int array[3];
void saveToFile();
void loadFromFile();
#ifdef __cplusplus
}
#endif
#endif
That provides for
- the contents of the header to be considered only the first time the header is included in any translation unit, and
- the
extern "C" declaration to be ignored unless the header is being processed by a C++ compiler.
After I try compiling it, table with "Source file not compiled" pops
up and I also get warning like this command line option '-std=c99' is
valid for C/ObjC but not for C++.
One does not compile headers directly. One uses #include directives to incorporate their contents into other sources. The source files you present contain such #include directives, so you should not need to do anything further in that regard.
In my file.c I have [...] undefined reference to `WinMain'
You are trying to compile file.c as a complete program, but that doesn't work because it does not have a main() function.
And in main.c [...] undefined reference to saveToFile', undefined reference to loadFromFile'
You are trying to compile main.c as a complete program, but it does not contain the source of a complete program because it calls functions that are defined in a different source file.
There are two main approaches you could take:
compile each .c file to an object file and then link them together into a whole program in a separate step, or
compile both .c files in a single compilation command.
Details of both options depend on the compiler and build system used. Some of your diagnostic messages suggest GCC to me, and with GCC running in command-line mode, the latter would be something like
gcc -std=c99 -o my_program.exe main.c file.c
. The "my_program.exe" is the name you want your executable to have. The -std=c99 is necessary with your code for some versions of GCC, and the diagnostics suggest that it appears in the command-line arguments you are actually using. You may include other command-line options, too, if you wish.
Note also that you must use a C compiler to compile C code, not a C++ compiler. Your diagnostics suggest that you may be trying to use the latter. C and C++ are different languages. Although they have a shared subset, neither is a superset of the other.