[C]redefinition of value?

Viewed 98

I'm now working on a project and trying to organize the working code for later reproduction. And then I encountered the famous redefinition error and unfortunately, none of the answers hit the spot. So I simplified my problem and here it is.

//main.c
#include <iostream>
#include "main.h"
#include "myfunc1.h"

int func2();
int func3();
int func4();

int main(int argc, char** argv) {
    
    func1();
    func2();
    func3();
    func4();
    
    return 0;
}

/* Trying to seperate func1*/

int func2(){
    int num2 = rep_val *2;
    int i = 0;
    for(i=0;i<num2;i++){
    printf("HO!\n");
    }
}
int func3(int rep){
    int num3 = rep_val *3;
    int i = 0;
    for(i=0;i<num3;i++){
    printf("HEY!\n");
    }
}
int func4(int rep){
    int num4 = rep_val *4;
    int i = 0;
    for(i=0;i<num4;i++){
    printf("WHAT?\n");
    }
}

So, all I want to do is to spread each func1,2,3,4 so main.c can remain clear. And following main.h , myfunc1.h, myfunc.c are what I did for func1.

main.h

int rep_val = 4;// myfunc1,2,3,4 will use this value so placed here

myfunc1.h

#include "main.h"

int num1 = rep_val*2;//rep_val = 4

int func1();

myfunc1.cpp

#include <myfunc1.h>

int func1(){
    int i = 0;
    for(i=0;i<num1;i++){
    printf("HI!\n");
    }
}

And the error was

D:\DEV\DEV C\Header_practice\main.h [Error] redefinition of 'int rep_val'

What am I missing? I've looked for several relevant queries and still can't find solution. Any word from you will be appriciated. Thanks!

Nam Jeongkuk

2 Answers

The solution is to add a header guard to main.h and myfunc1.h and make variable definitions extern (turn them into declarations).

main.h:

#ifndef __MAIN_H__
#define __MAIN_H__

extern int rep_val;

#endif

myfunc1.h:

#include "main.h"

#ifndef __MYFUNC1_H__
#define __MYFUNC1_H__

extern int num1;

int func1();

#endif

Alternatively, the #ifndef #define #endif sequences can be replaced by #pragma once with the most important compilers.

And then you add the variable definitions to the corresponding c files.

main.c:

#include <iostream>
#include "main.h"
#include "myfunc1.h"

int rep_val = 4;// myfunc1,2,3,4 will use this value so placed here
...

myfunc1.c:

#include <myfunc1.h>

// Cannot initialize this variable with other
// variable value. Can only provide default.
// Be aware of race conditions!
int num1 = 0;

int func1(){
    // but can update this variable from within function
    num1 = rep_val*2; //rep_val = 4
    ...
}

However, it is not regarded as good practice to place variables in file scope if not really necessary. Therefore i would suggest e.g. to put the num1 definition in the function func1 and remove it from the header at all. You may think about your context and where to place your variable definitions. If you have a good reason to provide a file scope variable then you'd go. The same may apply for rep_val.

First of all, the statement

 int num1 = rep_val*2;

cannot reside on file scope, it has to be in a function scope (for the platform to know when to execute it). This instance of rep_val is seen as it's own definition, which conflict with the other one in the other translation unit.

That said, without a header guard, you have multiple inclusion, which creates multiple definition of the same variable. Do not define the variables in header files, declare them in header file and define them in one of the translation units.

Related