#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    C Programming Question Regarding Splitting Program Up Into Multiple Files


    Alright, so I am having issues regarding splitting up a homework assignment of mine into a main.c file, a header.h file, and a functions.c file. So far, this is what I have saved into three separate codes.

    homeworkSixHeader.h
    Code:
    /*Structure for player's character info.*/
    struct playerInfo
    {
    	char name[25];
    	int lives;
    	int goldCollected;
    	int xLocation;
    	int yLocation;
    };
    
    /*Function prototypes*/
    void initializePlayer(struct playerInfo * character);
    void printPlayer(struct playerInfo character);
    void printBoard(char **gameBoard, int gameBoardSize);
    homeworkSixFunctions.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include "homeworkSixHeader.h"
    
    /*Function that initializes player information.*/
    void initializePlayer(struct playerInfo * character)
    {
    	srand(time(NULL));	
    
    	printf("\nWhat would you like your character's name to be?\n");
    	printf("We'll stay on a first name basis, just enter first name.\n");
    	scanf("%s",(*character).name);
    	(*character).goldCollected=0;
    	(*character).xLocation=0;
    	(*character).yLocation=0;
    	(*character).lives=rand()%50+1;
    }
    
    /*Function that prints player's data.*/
    void printPlayer(struct playerInfo character)
    {	
    	int i;
    
    	printf("\n\nYour character's name is: %s",character.name);
    	printf("\nThe amount of gold you've collected so far is: %d", character.goldCollected);
    	printf("\nThe amount of lives you have left is: %d", character.lives);
    	printf("\nYour player is located at coordinates (%d,%d)", character.xLocation, character.yLocation);
    }
    
    /*Function that prints the game board for the user.*/
    void printBoard(char **gameBoard, int gameBoardSize)
    {
    	int m,n;
    	
    	/*The use of '____' and '|' isn't necessary, but I find that it helps clean up
    	the output a bit and lets the user better see the game board and room contents.*/
    	printf("\n_________________________________________________________________________________________________________________________\n");	
    	for(m=0; m<gameBoardSize; ++m)
    	{
    		for(n=0; n<gameBoardSize; ++n)
    			printf("|__%c__", gameBoard[m][n]);
    		printf("|\n");
    	}
    	printf("\n");
    }
    homeworkSixMain.c
    Code:
    /*Purpose: The purpose of homework 6 is to develop the
      infrastructure for a program of a basic game. The program
      tests our use of functions, structures, arrays, and 
      the process of dynamically allocating memory.*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include "homeworkSixHeader.h"
    
    /*Main function*/
    int main(void)
    {
    	printf("\n\nWelcome to Homework Six! For this program,\n");
    	printf("we will be setting up everything needed to play a\n");
    	printf("simple game. The program will randomly generate a\n");
    	printf("20 x 20 game board, which will be populated with\n");
    	printf("blank rooms, rooms with gold, and rooms with a pit.\n");
    	printf("Blank spaces are the default, while gold has a .10\n");
    	printf("chance of appearing and pits have a .20 chance of\n");
    	printf("appearing. The player will be prompted to name their\n");
    	printf("character and the character will be placed at the\n");
    	printf("bottom-left corner of the game board. The player's\n");
    	printf("life count will be randomly generated and will be a\n");
    	printf("value between 1 and 50, and their gold amount will\n");
    	printf("start out at zero. The program will finish by printing\n");
    	printf("the player's starting info and the game board that was\n");
    	printf("generated. To save space on the game board, the player\n");
    	printf("will be represented by 'X', gold will be represented\n");
    	printf("by 'G', and pits will be represented by 'P'.\n");
    	
    	/*Seeding the random number generator with time.*/
    	srand(time(NULL));
    
    	/*Declaring the structure and calling the initialize function.*/	
    	struct playerInfo character;
            initializePlayer(&character);
    
    	/*Variable declarations.*/
    	int gameBoardSize=20;
    	char **gameBoard;
    
    	/*Keep getting a "segmentation fault, core dump" error that
    	I wasn't able to fix, so I am allocating memory for my game
    	board in main and will be filling its room contents here as 
    	well.*/
    
    	int i,j,k;
    
            /*Here, we are dynamically allocating memory for our 2d array,
            the game board, of size 20x20 with the use of calloc.*/
            gameBoard = calloc(gameBoardSize, sizeof(char *));
            for(i=0; i<gameBoardSize; ++i)
                    gameBoard[i] = calloc(gameBoardSize, sizeof(char));
    	
    	int percentChanceGold;
            int percentChancePit;
    
            /*Filling the game board with either a blank space, gold, or a
            pit. The chances of having gold in a room is 10% while the chance
            of having a pit is 20%. Since a room cannot have both gold and
            a pit, if the number generator has a result as such, the placement
            of gold takes priority of a pit.*/
    
            for(j=0; j<gameBoardSize; ++j)
                    for(k=0; k<gameBoardSize; ++k)
                    {	
    			if(j==(gameBoardSize-1)&&k==0)
    			{
    				gameBoard[j][k]='X';
    			}
    			else
    			{
                            	percentChanceGold=rand()%101;
                            	percentChancePit=rand()%101;
    
                            	if(percentChanceGold>=90&&percentChancePit<80)
                            	{
                                    	gameBoard[j][k]='G';
    				}
                            	else if(percentChanceGold<90&&percentChancePit>=80)
                            	{
                                    	gameBoard[j][k]='P';
                            	}
                            	else if(percentChanceGold>=90&&percentChancePit>=80)
                            	{
                                    	gameBoard[j][k]='G';
                            	}
                            	else
                            	{
                                    	gameBoard[j][k]='_';
                            	}
    			}
                    }
    
    	printPlayer(character);
    	printBoard(gameBoard,gameBoardSize);
    
    	return 0;
    }
    My issue isn't with my programming as far as I can tell (I had this all in one file before trying to split it up and it worked fine). I just can't seem to compile all three, and as a result I can't execute my main file. For reference, I am using PuTTY to compile and run my program. If anyone can point me in the right direction for what I need to do to alleviate this problem, any help would be greatly appreciated. Thank you for your time in reading this and any help you may be able to offer.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    PuTTY is just a communication application that runs ssh, as I recall. What compiler are you using? How does that compiler handle projects?

    I will assume that you are connecting to a Linux server and that the compiler that you are using is gcc. To compile multiple source files together with gcc, you simply include them all in the invocation. Hence:

    gcc -Wall homeworkSixMain.c homeworkSixFunctions.c -o hw6

    You don't need to name the header file, since that's #include'd into the source files; just ensure that it's in the same directory as the source files.

    Another approach would be to use a makefile and the make command. This is a more advanced approach, but it's widely used for large projects (some at work have well over 100 source files). Just be aware for now that it exists and you will learn about makefiles later when you need to.

    If you were using a compiler within an integrated development environment (IDE), then that IDE would have ways of creating a project and adding files to that project. The whole idea is to have some way of letting the compiler know which source files to compile and the linker know which object and library files to link together into the executable. Exactly how that is done depends directly on what tools you're using.

    BTW, FWIW, with gcc if you have a large number of source files, then a shortcut is to group all the source files into one directory and use a filename wildcard:
    gcc -Wall *.c -o myprog

    Of course, if you use a filename wildcard, then you must not have any other .c files in that directory that do not belong to the project.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    AH! Thank you so much; you're a life saver! And you guessed correctly, I am using a gcc compiler connected to a Linux server. I guess that would have helped to specify that for you, but you nailed it. I just didn't know the proper syntax on the command line to compile both c files together, so this was immensely helpful! Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo