In PHP you would simply do:
include("externalfile.php");
In C I have tried:
fopen("externalfile.c", "r");
externalfunction();
I give up, how do you run a function from an external file in order to keep the program compact and organized?
In PHP you would simply do:
include("externalfile.php");
In C I have tried:
fopen("externalfile.c", "r");
externalfunction();
I give up, how do you run a function from an external file in order to keep the program compact and organized?
You need to add the source file to your build.
What IDE/toolchain are you using?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I'm not sure what IDE/toolchains are, but I'm using:
Quincy.exe for my PC and
Xcode for my Mac OS X
I wouldn't think any of these things matter since the C source code is compatible with most operating environments (i read that somewhere).
PHP kicked my dog and destroyed the minds of past, present, and all future programmers who learned it first.
Maybe so, but the compiler isn't a mind reader. If you have multiple files in a project you should compile all of them, and then link the object files together. Since most compilers will call the linker / build an executable for you, it's usually a simple shell jobI wouldn't think any of these things matter since the C source code is compatible with most operating environments (i read that somewhere).
Maybe something like
C:/>quincy foo.c bar.c -o cprog
or a click of a button. But foo and bar (as well as other files) need to be part of your project.
I'm still a biginner so I'm not sure how large C projects are handled, but I'm getting the impression that this is more trouble than it's worth (no offense intended).
Worse case scenario, I can simply do this:
I know this works because I've tried it, the only problem is the single file becomes to large and not very organized. Spanning the code across multiple organized files would help a lot for bigger projects.Code:function1() { codex; } function2() { codey; } int main(void) { function1(); function2(); return 0; }
The reason why I need this is because I'm working on a school assignment which requires me to write a program which guesses magic squares.
They matter in the way that you configure each IDE to translate the portable C source code. This is the heart of your issue.
The short answer is, Read The Fine Manual.
An example might be this -- but that was for MSVC6.
- Xcode
- Quincy
- Find the "Adding Files To a Project" section in the help file
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I'm not sure what the question is. You can always call functions in other files, provides the object code from those files is linked as part of the linking step. Hell, you can do this even if you fail to correctly prototype the function (although that's a bad thing).
If your program is composed of the three files a.c, b.c, c.c, then all you have to do is compile them all and link them all together. There's nothing special about it.
Well, you usually need to use projects to compile multiple source files in an IDE, and if you've never used projects before that might constitute "special". Dev-C++, for example, can compile one source file all by its lonesome self; but you need to use a project to compile and link several files together.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.