• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NUCLEAR RABBIT

C Programming - .h and .c file Question

4 posts in this topic

Hello,

 

So I am just messing around using separate files to keep the main.c file not so cluttered up. It work's but I thought that it wouldn't. Maybe I am misunderstanding how it works, but for some reason, I did not #include the "Employee_def.c" file, yet the main.c file still knows what that function is, I thought I'd have to #include the Employee_def.c file.

 

Also, I am getting a warning in the main.c file on the line I call the Employee_Initializer function that states "Implicit declaration of function "Employee_Initializer" is invalid in C99". Still works, but not sure what that warning is trying to say. 

 

ANY help with an explanation or protips would be greatly appreciated! biggrin.png

 

main.c

#include <stdio.h>
#include "Employee.h"

////////////////////////////////////////////////////////////////////
//
//
//   MAIN
//
//
////////////////////////////////////////////////////////////////////

int main(int argc, const char * argv[])
{
    Employee emp;
    Employee * emp_ptr = &emp;
    Employee_Initializer(emp_ptr);
    
    return 0;
}

Employee.h

typedef struct {
    int age;
    int id;
    char sex;
    float pay_rate;
    float vac_hours;
    float sick_hours;
    float personal_hours;
    char first_name[20];
    char last_name[20];
    char job_title[25];
    char address[55];
} Employee;

Employee_def.c

#include <stdio.h>
#include "Employee.h"

// Employee Inializer
void Employee_Initializer(Employee * emp) {
    emp->pay_rate = 10.25;
    emp->vac_hours = 0;
    emp->sick_hours = 0;
    emp->personal_hours = 0;
    emp->age = 0;
    
    do {
        printf("Enter New Employee's Age: ");
        scanf("%d", &emp->age);
    } while(emp->age < 18);
    
    printf("Age: %d\n", emp->age);
}
0

Share this post


Link to post
Share on other sites

The warning is because main.c doesn't know about Employee_Initializer(Employee*). Add "void Employee_Initializer(Employee*);" to the end of Employee.h and it should be fixed.

0

Share this post


Link to post
Share on other sites

Older versions of C automatically assumed any undeclared function you call would exist in some other translation unit (roughly, any other .c file you link in to your program), though for various reasons this can be dangerous (the function may not have the signature or calling convention that the compiler assumed it would have).  That's why it works: it's terrible, horrible, dangerous practice, but it's legal in ANSI C.

 

C99 deprecated this feature, hence the warning.  It still works in many compilers with loose warning levels for backwards-compatibility, but it was deprecated for a reason.  Don't rely on it.

1

Share this post


Link to post
Share on other sites

1) Maybe I am misunderstanding how it works, but for some reason, I did not #include the "Employee_def.c" file, yet the main.c file still knows what that function is, I thought I'd have to #include the Employee_def.c file

 

The compiler works by first compiling the individual source(.c) files. It creates intermediate object(.o) files. Then the linker ties together all the object(.o) files to create the final executable. So when you    #include "Employee.h"   you are telling the compiler/linker "this source file needs to be linked with the .o file generated by Employee.c".

 

 

2)Also, I am getting a warning in the main.c file on the line I call the Employee_Initializer function that states "Implicit declaration of function "Employee_Initializer" is invalid in C99". Still works, but not sure what that warning is trying to say. 

 

You didn't declare the function in your header file. The compiler allows it, but it's very dangerous!

 

 

 

Protip: Include Guards are important, use them!

Edited by NGB_82
-1

Share this post


Link to post
Share on other sites

So when you #include "Employee.h" you are telling the compiler/linker "this source file needs to be linked with the .o file generated by Employee.c".

 

Not exactly. #include is literally copying the contents of Employee.h and pasting it in the file that invokes it, it doesn't tell the compiler or the linker anything.

 

Using your example, your main.c should be:

#include <stdio.h>
 
// forward declaration, so that the compiler is informed that this function is defined somewhere.
void Employee_Initializer(Employee * emp);
 
int main(int argc, const char * argv[])
{
    Employee emp;
    Employee * emp_ptr = &emp;
    // since the function is declared up there, it is ok to call it.
    Employee_Initializer(emp_ptr);
 
    return 0;
}

When you compile, you compile each .c file separately, producing a .o file.

When compiling main.o, it will see that forward declaration, so it will know that Employee_Initializer is either not defined yet, or it is in another .o file, and it won't throw any errors at you.

(for example: cc -c main.c -o main.o -std=c99)

 

Then you compile Employee_def.c and get Employee_def.o.

(for example: cc -c Employee_def.c -o Employee_def.o -std=c99)

 

Then you link main.o and Employee_def.o together, and the linker should be able to find Employee_Initializer in Employee_def.o.

(for example: cc main.o Employee_def.o a.out)

 

But what if you have more than one forward declaration? What if you have hundreds of lines of these forward declarations? It's tedious to copy and paste that to every file that needs them, so that's why #include is used. It allows you to paste as much info as you want from another file, without making your own file looking cluttered with forward declarations.

Edited by ultramailman
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0