Thread: trying to split some code into two files, error

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    80

    trying to split some code into two files, error

    Just tried putting a handful of functions into another file, so rather than having one .c file, have two .c files and a .h file.

    Cut handful of functions, put them into the second .c file. Cut their declarations, put those in the .h file. Put
    #include "second-file.h"
    at the top of the first .c file

    I'm getting error: expected ) before * token errors for each function but they were fine before I put them in the second file. What's going on there? Don't understand how putting them in another file could make any difference?

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    extra related but totally different Q: should #defines associated with the functions I'm putting in the second files, be in the .h or .c file? Probably doens't matter technically, but what's the best practice?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Post the smallest and simplest compilable program that works when it is a single file, and also post the same program split into two files, such that the split demonstrates this error message that you encountered.

    You probably made a typo error or something somewhere, but without code to see, I can only vaguely tell you that you made a mistake, which you already know to be true.

    Quote Originally Posted by BpB
    should #defines associated with the functions I'm putting in the second files, be in the .h or .c file? Probably doens't matter technically, but what's the best practice?
    It depends on whether they are meant to be "internal" or "external" to the translation unit. If they are "external", then they go in the header, otherwise they might go in the source file instead, with extra care for appropriate naming conventions to avoid name collision.
    Last edited by laserlight; 04-08-2017 at 02:00 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    > Post the smallest and simplest compilable program that works when it is a single file, and also post the same program split into two files, such that the split demonstrates this error message that you encountered.

    I was hoping I wouldn't have to do that; thought maybe it'd be known from what I said but I understand it's not. Am making a stripped down version now.

    >
    You probably made a typo error or something somewhere

    Pretty sure it's not (I just cut and paste), it'll be position (which files) of various things, I'm sure. Logistics/organisation of it. I don't really understand the organisation of it. Eg the way you've got to put both c file names on the command line: that seems wrong. Seems like they should get pulled in.

    >
    It depends on whether they are meant to be "internal" or "external"

    Right, thought it might be something like that.

    >
    If they are "external", then they go in the header, otherwise they might go in the source file instead, with extra care for appropriate naming conventions to avoid name collision.

    Right, so each c file is self-contained is it? That makes sense I suppose, but even that wasn't clear to me.

    Thanks.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Stuff that's defined in the first main c file, should that be also in a .h file and include it from the second .c file?

    So not
    test1.c (with main() and include test2.h)
    test2.h
    test2.c

    But
    test1.h
    test1.c (with main() and include test2.h)
    test2.h
    test2.c (with include test1.h)
    Last edited by BpB; 04-08-2017 at 02:24 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by BpB
    Stuff that's defined in the first main c file, should that be also in a .h file and include it from the second .c file?
    That depends on what you have in mind for the organisation of your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Well at the moment I just want it to work/compile (while in two .c files not just one) at the moment.

    Anyway, will do a simple test version hopefully, and post that.

    Thanks.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could start with this simple organization and then modify it as you see fit:

    main.c <-- this #include's other .h files

    module1.h
    module1.c

    module2.h
    module2.c

    ...

    Compile each one and link them together:
    Code:
    bash$ gcc -c module1.c -o module1.o
    bash$ gcc -c module2.c -o module2.o
    bash$ gcc -o main main.c *.o
    bash$ ./main
    Each module looks something like this:

    Code:
    #include "module1.h"
    #include <stdio.h> // If your module needs stdio, etc.
    // etc...
    Each module header looks like this:

    Code:
    int f1(); // E.g. this function defined in module1.c
    And main looks like this:

    Code:
    #include "module1.h"
    #include "module2.h"
    //...
    //etc
    Once you have that basic structure, rename things, add on and remove as desired to make whatever organization you want. It is convenient to add include guards to header files so that the ordering does not matter. The following is the unofficial way to make an include guard. It is unofficial but every compiler supports it:

    Code:
    #pragma once
    // Rest of header file

  9. #9
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    OK, thanks for that c99tutorial.

    I've got it. Was making a stripped down version and realised, the problem was, I'm pretty sure, a typedef not in the .h file seen by both .c files. I had left it in the original .c file. The split version is compiling OK now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2017, 08:39 PM
  2. How to split a C program into several files?
    By Romyo2 in forum C Programming
    Replies: 4
    Last Post: 06-20-2015, 08:45 PM
  3. split pdf files with ghostscript API
    By zoidberg in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2010, 04:52 PM
  4. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  5. HTML page split into files in c
    By Munisamy in forum C Programming
    Replies: 2
    Last Post: 02-21-2005, 05:58 AM

Tags for this Thread