Thread: Can I have two source files in one project in Code:Blocks

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    116

    Can I have two source files in one project in Code:Blocks

    Hi guys, I just downloaded Code:Blocks and made a C source file and a C++ source file, both just had hello world code.

    I wanted to just see the differences in the code and test that my IDE/Compiler etc was all working well.

    I am getting issues running the file. Both codes in the project define a main function. Will that cause an issue? And when I build and run, will it try to run both source codes or just the one I right clicked on?

    Also I changed the project and filename after building so not sure if that messed things up.

    Am I missing something or is there something else I need to do or am I doing something wrong?

    Also I installed the version with a GCC compiler. Is the GCC compiler installed as a separate entity? Can I run C code from the command prompt without using an IDE? If so, how do I do it?

    Sorry for all the noob questions. Just getting started and trying to get familiar with the IDE and compilation process etc before I start going into more detail with the code itself



    Can I have two source files in one project in Code:Blocks-problem-1-screenshot-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    > I am getting issues running the file. Both codes in the project define a main function. Will that cause an issue?
    It's always the case that you can only define each symbol once.

    > And when I build and run, will it try to run both source codes or just the one I right clicked on?
    You can compile a single file, but that only checks the file is syntactically valid. It doesn't produce a runnable program.


    > Is the GCC compiler installed as a separate entity?
    Yes (well kind of).
    For example, it's in C:/Program Files (x86)/CodeBlocks/MinGW on my Windows machine.

    > Can I run C code from the command prompt without using an IDE?
    Yes.

    > If so, how do I do it?
    You just have to add the directory to your PATH.
    Code:
    PATH=C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\Program Files (x86)\CodeBlocks\MinGW;%PATH%
    g++.exe -std=c++11 main.cpp
    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.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Ok, I have a book on C programming with over 100 code examples. I wanted to put them all into a project. Would I need to make a separate project for each example of code or can I have multiple c files in one project and be able to run them all independently?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    You could rename all the mains to main1, main2, main3 etc, then create a top-level main() which calls the mainx for the example you're interested in.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Ok that sounds like a good solution. I can make like a table of contents page essentially as main main and then call upon different source files to run and test the code I am interested in

    Is it as simple as just mentioning the functioning you want to call? Could you suggest some code I could use as a table of contents?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Here's one approach.
    Code:
    #include <stdio.h>
    
    // Probably in separate source files
    int main1(int argc, char *argv[] ) {
        printf("This is example 1\n");
        return 0;
    }
    
    int main2(int argc, char *argv[] ) {
        printf("This is example 1\n");
        return 0;
    }
    
    // In your new menu main
    
    extern int main1(int argc, char *argv[]);
    extern int main2(int argc, char *argv[]);
    
    struct {
        const char *title;
        int (*fn)(int, char*[]);
    } menu[] = {
        { "Example 1", main1 },
        { "Example 2", main2 },
    };
    
    int main (int argc, char *argv[]) {
        for ( size_t i = 0 ; i < sizeof(menu)/sizeof(*menu) ; i++ ) {
            printf("%zd %s\n", i, menu[i].title );
        }
        printf("Choose > ");
        int choice;
        scanf("%d",&choice);
        return menu[choice].fn(argc, argv);
    }
    The function pointer on line 21 is a whole rabbit hole you might want to avoid going down for the moment.
    So long as all your mainx functions have the same prototype, you can just go with it.


    Note that if you're calling any C++ code at all, then your actual main() function needs to be compiled in a .cpp file.
    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.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Ah ok so a C++ file can still understand C as well as C++?

    Thanks I will give your code a go and see how I get on with it

    Can I give functions decimal point names?

    E.g, int 2.1

    Also random question, why are functions declared in the same way as integer variables?

    Also, if main1 and main2 in your example are in different source files, do I need to do anything additional to make C look in the different source files? Or will it literally just go through every source file and look for any function that is called upon?

    If so, will having hundreds of source files make the programme take a long time to compile and run since it will need to "look through" all those source files?

    And how do I tell code:blocks which source file is the main menu source file? Will it automatically look for a function called main as the first function to run?
    Last edited by David_Baratheon; 02-15-2022 at 01:31 AM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    817
    Quote Originally Posted by David_Baratheon View Post
    Ah ok so a C++ file can still understand C as well as C++?
    Yes, but with some caveats (e.g., you can't name a variable "new").

    Can I give functions decimal point names?

    E.g, int 2.1
    No, that's not a valid identifier name. All identifiers must begin with an underscore or letter and can be followed by underscores, letters, or digits. A period is right out. (I'm not a language lawyer, so other characters may be valid in an identifier, such as a dollar sign, though I think that's a compiler extension).

    Also random question, why are functions declared in the same way as integer variables?
    A function is declared by the type that it returns (which could be int or other types) plus any parameters that it takes. An integer variable declaration is different since it doesn't have a parameter list. It's just how the language designers made it.

    Also, if main1 and main2 in your example are in different source files, do I need to do anything additional to make C look in the different source files? Or will it literally just go through every source file and look for any function that is called upon?
    You should have a header file (or multiple header files, perhaps one for each source file) with the functions declared, and include that header file wherever the function declaration is needed (i.e., in any source file that calls or uses a function that is declared in the header file). The linker will find the functions at link time.

    And how do I tell code:blocks which source file is the main menu source file? Will it automatically look for a function called main as the first function to run?
    The main function is always the entry point to a C/C++ program (except in special situations such as in a freestanding environment, but you probably won't encounter something like that unless you work with embedded software).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create project in code blocks
    By Ali Limbe in forum C Programming
    Replies: 1
    Last Post: 12-09-2021, 06:57 AM
  2. Creating My First Project with Code::Blocks
    By GokhanK in forum C Programming
    Replies: 3
    Last Post: 05-26-2018, 04:01 PM
  3. how to create a project in Code::Blocks?
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2011, 07:27 PM
  4. Multiple source files in one project
    By mintsmike in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2009, 07:20 AM
  5. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM

Tags for this Thread