Thread: How to build a C project?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    How to build a C project?

    I always wrote C programs limited to a single .C file including some standard .H; now I need to write a larger project, including several .C files, plus some .H written by me; I read several tutorials, bu I can't find anything about "linkage" more than "you need to link files together". Ok, but, HOW?

    I have to extend an existing project consisting of several .c and .h files; I wrote, let's say, test.c and test.h . Then I modified an existing function.c file, which now refers to test.h for the structure used by test.c to read and interpret a configuration file.

    I wrote a separate main2.c file which starts the function inside test.c which reads the file, and then calls the new-added functions in function.c which process the data.

    I am using Dev-C++, and if I edit single files and compile main2.c all work fine; but if I create a project which includes all files (new and old ones), compilation files: compiler complains about missing links to files, or double-links once I add the missing one! So, I don't understand where I should add proper #include statement. Or maybe it's just a mis-configuration of the program, or of the project?

    What I actually need is the original project to be able to read the configuration file, through my additions, before doing anything else.

    I know it sounds a bit confusing... that's why I'm here asking for help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well what error messages do you get when you have a project containing
    main2.c test.c function.c

    For example, if main2.c and test.c contain a main() for example, then one of them has to be renamed when you put both files into the same project.

  3. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    I don't know anything about Dev-C++ but you should be able to create a makefile and then use that. Look through your docs.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    Quote Originally Posted by Salem
    Well what error messages do you get when you have a project containing
    main2.c test.c function.c

    For example, if main2.c and test.c contain a main() for example, then one of them has to be renamed when you put both files into the same project.
    For example, a constant defined in main2.c is not visible in any other of the files! How can I make a constant global?!? I thought a constant was ALWAYS global!

    If I compile main2-c out of a project, ALL files compile fine; but if I add all files into a project, when I try compiling main.c the compiler complains about test.c not finding declaration for a global constant defined in main.c !
    But main.c starts with:

    #include "test.c"
    #include "function.c"
    #include "test.h"


    Actually my program is the only one where I've ever seen .C files included... so there should be something I don't understand here...

    Let's summarize:

    test.h holds the structure to read test.txt configuration file;
    test.c holds the procedure to read the conf file;
    function.c holds functions which use parameters read from test.txt;
    main2.c should call functions in function.c and display the values they return.

    So, how should I organize my sources into a project?? Which #include statement should I use, and WHERE?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Don't include .c files inside other .c files.
    Add them to the project.

    > For example, a constant defined in main2.c is not visible in any other of the files!
    Move it to a shared header file then.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    Thaks, it works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't build project in KDevelop C/C++
    By Lauris in forum Linux Programming
    Replies: 21
    Last Post: 09-04-2007, 10:41 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. I Need The Simplest Tutorial Out there.
    By yakabod in forum C++ Programming
    Replies: 8
    Last Post: 08-14-2003, 12:33 AM
  5. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:16 PM