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

AI Thread Summary
The discussion revolves around the organization and compilation of a C program that consists of multiple files. The main function is located in one .c file, while data structures are defined in another .c file, with input being read from a .txt file. It is confirmed that the program can run with this structure. A sample code snippet is provided, demonstrating how to read input from a file and use defined structures.The conversation shifts to compilation queries, specifically how to compile the program when additional functions are defined in a separate .c file. The user encounters errors related to undefined references when attempting to compile only the main.c file. The solution provided is to compile both main.c and stars.c together using the command "gcc main.c stars.c," which resolves the linking issues. The discussion concludes with an acknowledgment of the solution.
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)
 
Back
Top