How to build multi file program using gcc???

This is a discussion on How to build multi file program using gcc??? within the C Programming forums, part of the General Programming Boards category; So i have 3 files. main.c, test.c and test.h main.c contains: Code: #include "test.h" #include <stdio.h> and code test.c Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    How to build multi file program using gcc???

    So i have 3 files. main.c, test.c and test.h

    main.c contains:
    Code:
    #include "test.h"
    #include <stdio.h>
    and code
    test.c
    Code:
    #include "test.h"
    and code
    test.h
    Code:
    typedef struct {
    int test1;
    int test2;
    int test3;
    } test;
    How would i build this???

    i tried gcc -o main.c test.c test.h
    but i get a compiler error
    Last edited by SuperMiguel; 03-28-2012 at 02:30 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    gcc -o prog main.c test.c
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by Salem View Post
    gcc -o prog main.c test.c
    i did that and i get:

    Code:
    test.c: In function ‘input_test’:
    test.c:21: warning: incompatible implicit declaration of built-in function ‘scanf’
    test.c:21: error: ‘test1’ undeclared (first use in this function)
    test.c:21: error: (Each undeclared identifier is reported only once
    test.c:21: error: for each function it appears in.)
    test.c:21: error: ‘test2’undeclared (first use in this function)
    test.c:21: error: ‘test3’ undeclared (first use in this function)
    test.c:22: warning: ‘return’ with a value, in function returning void
    Code:
    test.c:21: warning: incompatible implicit declaration of built-in function ‘scanf’
    on main.c there is #include <stdio.h>, so why would i get this error??

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Because those errors are in test.c. If you plan on using functions from stdio.h in test.c, then you must #include it in test.c.

    As a side note, it would be good to get in the habit of turning the warnings all the way up, and make sure you fix them all. Just add a -Wall to the commandline:
    gcc -Wall -o prog main.c test.c

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    how about
    Code:
    test.c:21: error: ‘test1’ undeclared (first use in thisfunction)
    
    test.c:21: error: (Each undeclared identifier is reported only once
    test.c:21: error: foreach function it appears in.) test.c:21: error: ‘test2’undeclared (first use in thisfunction) test.c:21: error: ‘test3’ undeclared (first use in thisfunction)
    they are defined in test.h
    Code:
    typedef struct {
    int test1;
    int test2;
    int test3;
    } test;

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Well, I can't say for sure, since you didn't actually post any code for test.c. I'm guessing you're not using them as members of a struct. They do not work as variables on their own when declared inside a struct like you did.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by anduril462 View Post
    Well, I can't say for sure, since you didn't actually post any code for test.c. I'm guessing you're not using them as members of a struct. They do not work as variables on their own when declared inside a struct like you did.
    weird that error just went away.. the code is

    Code:
    void input_test(test *dp)
    {
    scanf("%d", &test.test1);
    return;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    That should not actually work. test is the name of a type, not a variable. You can't reference a member (e.g. test1) of the type itself. You must reference the test1 member of a variable. Something here doesn't add up. You're not showing the full code (the entire contents of every file) exactly as it is on your system, or you've changed something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program compiles; won't build
    By Kayoss in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2006, 09:46 AM
  2. Build music into a program
    By XunTric in forum C++ Programming
    Replies: 12
    Last Post: 01-10-2006, 07:16 AM
  3. Multi - File Program not finding files
    By johnh444 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2004, 01:48 AM
  4. How to build Multi Form?
    By NightWalker in forum Windows Programming
    Replies: 2
    Last Post: 05-08-2004, 09:08 AM
  5. Multi file program?
    By zolo44 in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21