Thread: Function from external source file

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    Function from external source file

    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?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    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).

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    PHP kicked my dog and destroyed the minds of past, present, and all future programmers who learned it first.
    I wouldn't think any of these things matter since the C source code is compatible with most operating environments (i read that somewhere).
    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 job

    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    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:

    Code:
    function1()
    {
      codex;
    }
    
    function2()
    {
      codey;
    }
    
    int main(void)
    {
      function1();
      function2();
    
      return 0;
    }
    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.

    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.

  6. #6

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by verbatim210 View Post
    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).
    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.
    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.*

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. VC++ a POS
    By Welder in forum Windows Programming
    Replies: 40
    Last Post: 11-07-2007, 03:07 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM