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

Click For Summary

Discussion Overview

The discussion revolves around the feasibility of running a C program where the main function and data structures are defined in separate files. Participants explore the implications of including additional source files and how to compile them correctly, addressing issues related to undefined references during compilation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants inquire whether it is possible to run a program with the main function in one file and data structures defined in another file, expressing curiosity about the setup.
  • One participant provides a sample structure for the program, including a header file and main function, suggesting that it should work as intended.
  • Another participant asks about the compilation command, specifically whether using "gcc main.c" is sufficient.
  • Concerns are raised about including an additional source file (stars.c) containing functions, with one participant reporting compilation errors related to undefined references to various functions.
  • A later reply suggests the correct compilation command should include both main.c and stars.c to resolve the undefined references issue.

Areas of Agreement / Disagreement

Participants express uncertainty about the compilation process and the necessity of including multiple files. While some provide solutions, there is no consensus on the best approach to resolve the compilation errors.

Contextual Notes

The discussion highlights potential limitations in understanding how to properly link multiple source files during compilation, as well as the need for clarity on function definitions and declarations across files.

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
86
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · 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 57 ·
2
Replies
57
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K