Reputation: 95
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
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++.
In my file.c I have:
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
void saveToFile(){
FILE *load=fopen("1uzd.txt", "w");
int array[3];
fprintf(load, "%d ", 3);
array[0]=1;
array[1]=2;
array[2]=3;
for(int i=0; i<3; i++){
fprintf(load, "%d ", array[i]);
}
fclose(load);
}
void loadFromFile(){
FILE *load=fopen("1uzd.txt", "r");
int size;
fscanf(load, "%d", &size);
int array[size];
for(int i=0; i<size; i++){
fscanf(load, "%d", &array[i]);
}
printf("%d %d %d", size, array[0], array[1]);
}
And undefined reference to `WinMain' with ld returned 1 exit status And in main.c:
#include <stdlib.h>
#include <stdio.h>
#include "file.h"
int main(void){
saveToFile();
loadFromFile();
return 0;
}
With undefined reference to saveToFile', undefined reference to
loadFromFile' and ld returned 1 exit status.
Upvotes: 1
Views: 1118
Reputation: 180048
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
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.
Upvotes: 2