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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
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)
 
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)
 
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)