locked
compiling c code between multiple files RRS feed

  • Question

  • Using VS 2005, I want to split my C code into multiple files. I have about 3000 lines of code and I want to split this into multiple files to share functions between projects.

    I can do this using #include "secondfile" within "firstfile", but I am unable to compile the code if I include "secondfile" in the project (Project>Add Existing Item>secondfile). When I try to include "secondfile", I get lots of compile errors. If I remove "secondfile" from the project, the project compiles fine.

    If I remove "secondfile" from the project, I am able to compile, but it is inconvenient to browse to find the file to open it. Also, with the file excluded, the functions are not recognized as being within the global scope of the project.

    Note that "secondfile" cannot be compiled independently of "firstfile", so I don't think that I can set it up as a second project?

    This seems like a simple question, but I can't find it addressed in user manual, help files, or forums.
    Tuesday, May 8, 2007 9:17 PM

Answers

  • You never include a c source file without good reasons.

    If you want to use a standard set of functions in a program, the way to do it is follows.

     

    1) Put all function prototypes into a header file. This is done so the compiler can resolve the functions but don't actually have a body of the function yet.

    2) Split your source files however you want them to be split.

    3) Include this header in all of your source files.

     

    This is the proper way of doing it. Although my instructions are pretty simplistice here. This is not an area that anyone can say there is a correct way of doing things. You will have to experement with this yourself and play around until you get it how you like.

     

    Adding this to a project has two possible methods. You can first compile it and create a library (lib or dll) out of it, then you link this into your project. Or you can add the source files themselves to the project and compile them along with the project. Either way, the header file you created right at the beginning is added to the project and included into each source file in the project which needs one of the functions in this standard set of functions.

    Tuesday, May 8, 2007 10:44 PM
  • You shouldn't need to worry about including the same header file in different source files that is what they are there for. Header files should only be used for definitions that way you can include them in as much as you need without problems.

    There can be some problems with including files, you may have already met with two of them I suspect. These are multiply declared symbols and recursive includes. But with good programming practice there shouldn't be any problems with including these headers twice.

    If you want to use global variables, don't put them in the header files, this will cause you problems because you will have the multiple symbols error from the linker. The way you should do it is to have the variable in only one source file and in the header you should then have an extern for it.

    As far as compiling is concerned though, if you have an include file in seperate source files it will not cause problems. A well written include file will also not cause problems if included more than once in the same source. The compiler only sees things on a per source basis, so the compiler doesn't know or care if a header file is included in any other sources. Its job is to parse the file and output the object file. The linker also shouldn't have problems, the problems start appearing if you put actual functions or variables in header files.

    Wednesday, May 9, 2007 12:37 AM

All replies

  • You never include a c source file without good reasons.

    If you want to use a standard set of functions in a program, the way to do it is follows.

     

    1) Put all function prototypes into a header file. This is done so the compiler can resolve the functions but don't actually have a body of the function yet.

    2) Split your source files however you want them to be split.

    3) Include this header in all of your source files.

     

    This is the proper way of doing it. Although my instructions are pretty simplistice here. This is not an area that anyone can say there is a correct way of doing things. You will have to experement with this yourself and play around until you get it how you like.

     

    Adding this to a project has two possible methods. You can first compile it and create a library (lib or dll) out of it, then you link this into your project. Or you can add the source files themselves to the project and compile them along with the project. Either way, the header file you created right at the beginning is added to the project and included into each source file in the project which needs one of the functions in this standard set of functions.

    Tuesday, May 8, 2007 10:44 PM
  • This is very helpful. I am using header files for all the prototypes. My current objective is to pull out the I/O routines to share between different projects.  

     

    I want to confirm your instructions to use and #include in the top of each file. This is what I may need. I also need to declare global variables in the header file. I was concerned about including the file twice. Sounds like what I need? The compiler/linker doesn't get confused with #include statements for each of the source files?

     

    Tuesday, May 8, 2007 11:19 PM
  • You shouldn't need to worry about including the same header file in different source files that is what they are there for. Header files should only be used for definitions that way you can include them in as much as you need without problems.

    There can be some problems with including files, you may have already met with two of them I suspect. These are multiply declared symbols and recursive includes. But with good programming practice there shouldn't be any problems with including these headers twice.

    If you want to use global variables, don't put them in the header files, this will cause you problems because you will have the multiple symbols error from the linker. The way you should do it is to have the variable in only one source file and in the header you should then have an extern for it.

    As far as compiling is concerned though, if you have an include file in seperate source files it will not cause problems. A well written include file will also not cause problems if included more than once in the same source. The compiler only sees things on a per source basis, so the compiler doesn't know or care if a header file is included in any other sources. Its job is to parse the file and output the object file. The linker also shouldn't have problems, the problems start appearing if you put actual functions or variables in header files.

    Wednesday, May 9, 2007 12:37 AM
  • Great! I got it to work.

     

    I got away with global vars in the header file that is included in both source files, perhaps the VS linker is forgiving. I will look into the 'extern' statement because I need to recompile on a unix machine for production runs. So my code is pretty generic, plain jane, but this might cause problems with the pathcc compiler that I use there.

     

    Thanks so much for the tips. I'm a happy camper for now. VS finds all the global functions from the class view and opens the correct files. So I can debug.

    Wednesday, May 9, 2007 2:21 AM