Is it Possible? - Running a Program with Data Structures in Separate Files

Click For Summary
SUMMARY

This discussion confirms that it is indeed possible to run a C program with the main function located in a separate file from the data structure definitions. The program structure includes a header file "stars.h" for type definitions, a source file "main.c" for the main function, and an input file "input.txt" for data input. To compile the program correctly, users must include all relevant source files, such as "gcc main.c stars.c", to avoid undefined reference errors during linking.

PREREQUISITES
  • Understanding of C programming language syntax and structure
  • Familiarity with header files and their usage in C
  • Knowledge of file I/O operations in C
  • Basic command line usage for compiling C programs with GCC
NEXT STEPS
  • Learn about GCC compilation options and linking multiple source files
  • Explore C data structures and their implementation in header files
  • Investigate error handling in file I/O operations in C
  • Study modular programming in C to improve code organization
USEFUL FOR

C programmers, software developers, and students learning about modular programming and file management in C.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

I have the main function of a program in a .c file.
The main function uses data structures which are defined in an other .c file.
And the main function uses also a .txt file to get some inputs.

Is it possible to run the program if the main function is an other file as the definition of the structures?? (Wondering)
 
Technology news on Phys.org
mathmari said:
Hey! :o

I have the main function of a program in a .c file.
The main function uses data structures which are defined in an other .c file.
And the main function uses also a .txt file to get some inputs.

Is it possible to run the program if the main function is an other file as the definition of the structures?? (Wondering)

Hi! (Happy)

Sure.
Your program should look something like:

input.txt:
Code:
ss=1
ss=101

stars.h:
Code:
typedef struct starsy {
  int ss;
} starsy_t;

main.c:
Code:
#include <stdio.h>
#include "stars.h"

starsy_t StarS[N];
int nrStarS = 0;

int main() {
  FILE *fp = fopen("input.txt", "r");
  if (fp != NULL) {
    while (1 == fscanf(fp, "ss=%d", &StarS[nrStarS].ss)) {
      nrStarS++;
    }
    fclose(fp);
  }
  return 0;
}
(Wasntme)
 
I like Serena said:
Hi! (Happy)

Sure.
Your program should look something like:

input.txt:
Code:
ss=1
ss=101

stars.h:
Code:
typedef struct starsy {
  int ss;
} starsy_t;

main.c:
Code:
#include <stdio.h>
#include "stars.h"

starsy_t StarS[N];
int nrStarS = 0;

int main() {
  FILE *fp = fopen("input.txt", "r");
  if (fp != NULL) {
    while (1 == fscanf(fp, "ss=%d", &StarS[nrStarS].ss)) {
      nrStarS++;
    }
    fclose(fp);
  }
  return 0;
}
(Wasntme)

Ahaa... Ok! (Malthe)

And to compile it what do I have to write??

[m]gcc main.c[/m] ??

(Wondering)
 
mathmari said:
Ahaa... Ok! (Malthe)

And to compile it what do I have to write??

[m]gcc main.c[/m] ??

(Wondering)

Yep! (Nod)
 
Can it also be that besides [m]input.txt[/m], [m]stars.h[/m] and [m]main.c[/m] there is also an other .c file ([m]stars.c[/m]) where there are all the functions (Beginning(), Consitution(), destruction(), ... )?? (Wondering)

If so, how can I compile it?? (Wondering)

Because when I write [m]gcc main.c[/m] I get:

[m]
/tmp/ccacL3pX.o:main.c:(.text+0x122): undefined reference to `_Initialization'
/tmp/ccacL3pX.o:main.c:(.text+0x1a7): undefined reference to `_Constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x28a): undefined reference to `_Beginning'
/tmp/ccacL3pX.o:main.c:(.text+0x38b): undefined reference to `_asteroid_constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x455): undefined reference to `_destruction'
/tmp/ccacL3pX.o:main.c:(.text+0x542): undefined reference to `_asteroid_freefloating_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x676): undefined reference to `_freefloating_collection_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x76d): undefined reference to `_SolarSystem_Combiner'
/tmp/ccacL3pX.o:main.c:(.text+0x819): undefined reference to `_Search_asteroid'
/tmp/ccacL3pX.o:main.c:(.text+0x8cf): undefined reference to `_get_asteroids'
/tmp/ccacL3pX.o:main.c:(.text+0x97b): undefined reference to `_type_planetary'
/tmp/ccacL3pX.o:main.c:(.text+0xa1d): undefined reference to `_type_ffcol'
/tmp/ccacL3pX.o:main.c:(.text+0xabf): undefined reference to `_type_StarSystem
/tmp/ccacL3pX.o:main.c:(.text+0xb01): undefined reference to `_type_universe'
/tmp/ccacL3pX.o:main.c:(.text+0xb2c): undefined reference to `_termination'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: /tmp/ccacL3pX.o: bad reloc address 0x11c in section `.rdata'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
[/m]

(Wondering)
 
mathmari said:
Can it also be that besides [m]input.txt[/m], [m]stars.h[/m] and [m]main.c[/m] there is also an other .c file ([m]stars.c[/m]) where there are all the functions (Beginning(), Consitution(), destruction(), ... )?? (Wondering)

If so, how can I compile it?? (Wondering)

Because when I write [m]gcc main.c[/m] I get:

[m]
/tmp/ccacL3pX.o:main.c:(.text+0x122): undefined reference to `_Initialization'
/tmp/ccacL3pX.o:main.c:(.text+0x1a7): undefined reference to `_Constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x28a): undefined reference to `_Beginning'
/tmp/ccacL3pX.o:main.c:(.text+0x38b): undefined reference to `_asteroid_constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x455): undefined reference to `_destruction'
/tmp/ccacL3pX.o:main.c:(.text+0x542): undefined reference to `_asteroid_freefloating_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x676): undefined reference to `_freefloating_collection_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x76d): undefined reference to `_SolarSystem_Combiner'
/tmp/ccacL3pX.o:main.c:(.text+0x819): undefined reference to `_Search_asteroid'
/tmp/ccacL3pX.o:main.c:(.text+0x8cf): undefined reference to `_get_asteroids'
/tmp/ccacL3pX.o:main.c:(.text+0x97b): undefined reference to `_type_planetary'
/tmp/ccacL3pX.o:main.c:(.text+0xa1d): undefined reference to `_type_ffcol'
/tmp/ccacL3pX.o:main.c:(.text+0xabf): undefined reference to `_type_StarSystem
/tmp/ccacL3pX.o:main.c:(.text+0xb01): undefined reference to `_type_universe'
/tmp/ccacL3pX.o:main.c:(.text+0xb2c): undefined reference to `_termination'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: /tmp/ccacL3pX.o: bad reloc address 0x11c in section `.rdata'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
[/m]

(Wondering)

[m]gcc main.c stars.c[/m]
(Wasntme)
 
I like Serena said:
[m]gcc main.c stars.c[/m]
(Wasntme)

Ahaa... Ok! Thank you very much! (Yes)
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
86
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K