Thread: how to split long programe in small files

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    7

    Question how to split long programe in small files

    Hello friends,

    I am writing a C code...

    Now it become very long about 10000 lines...and have so many globle variables...cause these all variables are used in all user defined functions.....

    Now I am thinking to split in small files like a small programe as follow..It complie fine but not creating any build..in VC++ (I am using VC++ for C programing)...

    Main programe.....

    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<io.h>
    #include<process.h>
    #include<math.h>
    #include <malloc.h>
    #define _CRT_SECURE_NO_WARNINGS
    #include "resize.cpp"
    
    void main()
    {
    
    	printf("\ntest ");
    	resize_cell_pointer2();
    }
    small program for in resize.cpp file(I kept all *.cpp files in same folder)

    Code:
    void resize_cell_pointer2()
    {
    	printf("\ntest sucess");
    }
    This compile it fine but it doesnt Build....!!! I dont know why.....I tried lot but it is going me error messege as follow.

    >------ Build started: Project: 3d_new, Configuration: Debug Win32 ------
    1>Compiling...
    1>resize.cpp
    1>c:\users\umesh\program\3d_new\3d_new\resize.cpp( 5) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
    1>Build log was saved at "file://c:\Users\umesh\program\3d_new\3d_new\Debug\BuildLo g.htm"
    1>3d_new - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    Please help me for this small problem.

    Thanks in Advance
    Umesh Javiya

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main returns int - read FAQ - http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    you have 2 options -
    1. go to Project settings and disable the precompiled headers
    2. include the "stdafx.h" in any module included in yuor project

    (I always prefer the first one)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    The same applies to C as well.

    Even in C, you'll have certain key structures which have a set of "member" functions which manipulate those structures. These you can separate out into separate modules and header files.

    > Now it become very long about 10000 lines
    The underlying problem is that you haven't learnt how to program yet. Sure you may know enough syntax to be able to write any program in a couple of hundred lines of code. But your knowledge doesn't scale up to 10000+ lines very well. Programming large programs take a lot more thinking about than just typing the code.

    If this were a book, it would be a single block of text. Think about chapters and paragraphs. The same goes for code, think how you're going to organise the overall structure of the program.

    > small program for in resize.cpp file(I kept all *.cpp files in same folder)
    If you're really programming in C, then rename them to be .c files.

    > #include "resize.cpp"
    > c:\users\umesh\program\3d_new\3d_new\resize.cpp( 5)
    DO NOT include source files inside source files. The code for resize is going to get compiled TWICE if you do it like this. Sometimes you can get away with it, but most of the time you'll get a "multiply declared" error from the linker. Even when you don't, you'll end up with duplicate code in the executable.
    Your main.c file should have
    #include "resize.h"



    > #include<fcntl.h>
    > #include<io.h>
    > #include<process.h>
    > #include <malloc.h>
    Learn what headers are portable, and which are not. malloc() for example is prototyped in stdlib.h (which you include), so the ancient malloc.h is redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    Thank you very much fro reply....

    I am a new guy in C programing..... I dont do C programing for my engineering calculations.....

    So, Initially I written 10000 lines code to check my solution, I am sure that I can reduce it to 2000-3000 lines... by using advance tutorials on cprogramming.com.

    Now after spending enough time to get right results from calculations(getting right answer). I want to do calculation for optimisation purpose... so I want to writing in small modules so I do not have to go in changing main program, like user define functions (in separate files which can give a return value from desired equation).

    So, please let me know how can be done in easiest way to split long program in small files.

    I spent lots of time on searching this but it is not working out...

    Salem: The program is compiling only once... I checked it....

    Vart: I tried both stuff in your reply, but it is not working out....

    Thanks and regards,
    Umesh Javiya

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but it is not working out
    And how exactly it is not working out?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would imagine the fact that he is including source files as a reason for why its not working out.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    My main program file name is 3d_new.cpp which has the main() function as follow.

    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<io.h>
    #include<process.h>
    #include<math.h>
    #include <malloc.h>
    #define _CRT_SECURE_NO_WARNINGS
    #include "resize.cpp"
    
    void main()
    {
    
    	printf("\ntest ");
    	resize_cell_pointer2();
    }
    and another file 'resize.cpp' in same directory as follow.

    Code:
    #include "stdafx.h"
    
    void resize_cell_pointer2()
    {
    	printf("\ntest sucess");
    }
    I have include "stdafx.h" in both the files and switch off the disable the pre-compiled headers option also.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<io.h>
    #include<process.h>
    #include<math.h>
    #include <malloc.h>
    #define _CRT_SECURE_NO_WARNINGS
    #include "resize.cpp"
    Also, include all your header files in stdafx.h (its a microsoft thing) and then you can enable the precompiled headers and having things compile a little faster.

    [edit]One important thing that I should mention is that including a source file is bad form. However, its not necessarily going to ruin your life if
    - Its ONLY included once.
    - You don't try to build the file by itself.

    EIther way, you shouldn't get into the habit of doing that sort of thing.
    [/edit]

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    I also thinking same way.....

    Cause I tried the same program with out any change on Linux with gcc command ( I dont details about compiler)

    I am using microsoft VC++...so have to work with it some how....

    working files on linux as follow.

    File: 3d_new.c

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<math.h>
    #include "resize.c"
    
    main()
    {
    
    printf("\ntest ");
    resize_cell_pointer2();
    
    }
    File: resize.c


    Code:
    void resize_cell_pointer2()
    {
    printf("\ntest sucess");
    }
    I will try on VC++ and let u know.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, I hate doing this since a lot of the moderators whince when duplicate information fills the forums. So I will keep it short, sweet, and to the point. You can also search the forums for more details.

    You need to learn the art of creating headers. I sometimes do as you have done and just write the program in one giant source file, then extract headers from the existing code and move bits of code into a logical order.

    You need to make headers is the point. Prototypes for your functions. Prototypes for your globals (don't forget extern on these ones.)

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by umeshjaviya View Post
    I also thinking same way.....

    Cause I tried the same program with out any change on Linux with gcc command ( I dont details about compiler)
    Don't use precompiled headers if you are planning to use different compilers - precompiled headers is a MS thing, and it's not noticably faster unless you have MANY, BIG header files that are included in EVERYTHING - and good programming style should isolate machine dependant things such as "windows.h" to one or a few C files [at least if you wish to have SOME chance of porting it to another OS].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  3. fscanf won't work for long files
    By tscalfa in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 05:38 PM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM