0

I got a problem with including files. What I've basically made is a command-line program in C that works with a csv file. It has 5 commands: -menu, -add, -edit, -del, -verify. All is good except for the -menu. What needs to happen when I type "./passweb -menu" is for a visual menu to appear. This menu command should call a new function and it needs to be located in a separate c file (aka menu.c).

The problem I'm having right now is that I'm not too sure how to include the files to run in the menu.c because the way I have it set up right now, passweb.c has all the functions such as -add and -edit and etc. I know you need to make header files and such but it's complaining that I've declared the methods twice which is true since I use the functions in the menu file.

Here's some of the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cipher.h"
#include "menu.h"
#include "passweb.h"
#define MAXLENGTH 51 //longest length of a single record + 1 =51 bytes
#define SIZEOFDB 1000
#define ENCRYPT 5

typedef struct rec
{
    char name[MAXLENGTH];
    char pw[MAXLENGTH];
    char type[6];
    int boolean;
}RECORD;

int add(char *nName, char *nPw, char *nType, RECORD *arr, int size);
void del (char *tName, RECORD *arr, int size);
int edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr, int size);
int verify (char *tName, char *tPw, RECORD *arr,int size);

This is my main file "passweb.c" and it's declared the functions. Later on in the main, I will call a menu function which is located in a menu.c file. In the menu function I use "add","edit","del" and etc that I wrote in this .c file. As you can see on the top, I have included the header files.

What can I do to solve this problem? Sorry for the long post.

Thanks

3
  • What is your exact error message? What's in the header file that has the duplicate declaration? Mar 6, 2012 at 20:42
  • passweb.c:20: error: conflicting types for ‘add’ passweb.h:12: error: previous declaration of ‘add’ was here These come up for every function. Is it okay if I use a function created in, for example, main.c which runs a function in menu.c and then menu.c runs a function created main.c?
    – John Smith
    Mar 6, 2012 at 20:49
  • OK, that's better. You are declaring the function twice. Once in the header file and once in the C file. You can only declare something once per translation unit. Mar 6, 2012 at 20:55

4 Answers 4

1

You should put function prototype in the header file. You should put its implementation in the source file, which should include the header file.

For example, the header (test.h) would have the structure definitions and function prototypes like:

#ifndef TEST_H
#define TEST_H
struct myStruct {
  int x;
};

int Add(int lhs, int rhs);
#endif

And the source would have:

#include "test.h"
#include <stdio.h>
int Add(int lhs, int rhs)
{
  return lhs+rhs;
}
int main()
{
  printf("%d",Add(2,3));
  return 0;
}
4
  • If I have them in the header file, does that mean I don't need to prototypes in the c file?
    – John Smith
    Mar 6, 2012 at 20:48
  • @YamatoC - if you have what in the header, the implementation? If you have the implementation in the header, you do not need anything but the include in the .c file.
    – prelic
    Mar 6, 2012 at 20:51
  • I mean I have the prototypes in the header file as well. Does this mean I don't need the prototypes in the file they're implemented in?
    – John Smith
    Mar 6, 2012 at 20:55
  • @YamatoC - You do not need prototypes for a function in the same file it is implemented, unless you use the function before you've defined it. So for example, if main calls your function, your function either needs to be implemented above main, or you can stick a function prototype above main, and put the actual implemention (definition) below main. Basically, you're just declaring it so when the compiler gets to the function call in main, it'll see that the function has been declared (by the prototype above).
    – prelic
    Mar 6, 2012 at 20:59
1

put this in your header file and include it:

#ifndef FUNCS_H_
#define FUNCS_H_

int add(char *nName, char *nPw, char *nType, RECORD *arr, int size);
void del (char *tName, RECORD *arr, int size);
int edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr, int size);
int verify (char *tName, char *tPw, RECORD *arr,int size);

#endif /*FUNCS_H_*/

the #ifdef guards the header from multiple inclusion

1

Move the declaration of add, del, edit and verify in a new include file main.h

Include main.h in your main .c file and in menu.c

In general is also a good idea to put directives in the include files that tells the preprocessor to use them only once - Visual C++ has a special #pragma once directive for that, otherwise you can use

#if ! __MENU_C
#define __MENU_C

. . . .

#endif
1

The header file should contain your function prototypes and the necessary information about the data types they reference, such as your struct definitions. It may also include #define'ed constants, inline functions, declarations of extern variables (which would be declared normally inside one of your .c files), etc.

Once you have these things in your header file, you can remove them from the .c file(s); the actual function definitions, with the bodies, should be in the .c files -- but don't duplicate the prototypes there. You then #include the header in each .c file that needs the functions, etc. declared in it.

You also need to ensure that the items in the header file don't get #include'ed more than once in each source file, even indirectly (included from another include). Usually header guards are used for this, which use preprocessor conditionals to prevent processing of the header file when it has already been included:

#ifndef SOMEHEADER_H
#  define SOMEHEADER_H

/* header file contents */

#endif

When compiling, you should be able to compile each .c file separately if you like, or all together if the compiler lets you. When linking, you need to link all the object files though. For gcc, doing both in one command, you might use something like this:

gcc -o program_name first.c second.c third.c

...which compiles each .c file to its own .o file, and links all of them together when producing the executable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.