Question:
Coding in C using Visual C++. How do i make multiple .c files instead of one large one....?
BlahBluhBlahBluhBlah
2008-06-04 21:49:13 UTC
i dont know how. also , when i make them how exactly do i call them...

//THIS IS THE MAIN.C FILE
#include "stdafx"

void main()
{
//ok so now i want the program to go to the other .C source to
//execute the code there....how is this done.
}
Four answers:
2008-06-05 01:24:13 UTC
// main.c



#include "file1.h"

#include "file2.h"



void main()

{

f1();

f2(0);

}



// file1.h

void f1();



// file2.h

void f2(int);



// file1.c

#include

#include "file1.h"

void f1()

{

printf("f1 called\n");

}



// file2.c

#include

#include "file2.h"

void f2(int c)

{

printf("f2 called with parameter '%d'\n", c);

}
2008-06-04 22:02:16 UTC
Depends on your environment - if you are using a commandline like cc or gcc just add them to the command

cc main.c file1.c file2.c -o=exefile.exe



If you are using a development environment (visual/gui) most environments have the concept of a 'project' - and you "edit" the contents of the project by telling it exactly which files are to be used during the build.



You need add NOTHING to your main program to do this, other than references to the functions present in your other c-files. e.g. to your "main" program, add #include "file1.h" and #include "file2.h" where these .h files have prototypes for the functions in the separate files.....



HTH
?
2016-10-15 11:15:21 UTC
"rapid C++" replaced right into a compiler that predated the launch of the C++ language definition (1998) by utilising various years. maximum classes that are compiled by utilising rapid C++ are no longer quite C++ classes. in specific, is a header record that does no longer exist in C++. there's a header record called iostream quite, in spite of the undeniable fact that it quite is basically the top of the iceberg. "seen C++" is a competent, cutting-part compiler (exceedingly in case you are able to swap to 2010 or 2012 version), which helps prevalent C++, as nicely as countless Microsoft extensions. you do no longer might desire to apply any of the extensions (all that _tmain and stdafx stuff) once you're in simple terms getting to grasp the language.
2008-06-04 21:56:11 UTC
You write the other.c file. It will have global variables and functions. Put the function headers in other.h to declare them.



Then do

#include "other.h"



You will now be able to call the other file's functions.



The linker will take care of combining them into one program.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...