0

I have a header file command.h which contains all my variables and function declarations

//command.h 

int someVar1; 
int someVar2;
void modifying_loop (int a, int b);
int someVar3;
.
.
.

In another file my_algorithm.c I define the previously declared function modifying_loop and use some of the variables declared in the header in

//my_algorithm.c

#include "command.h"
void modifying_loop (int x, int y)
{
    someVar1 = x+2;
    someVar2 = y+2;
}

And I have my main file command.c I called the modifying_loop function like this :

#include "command.h"
int main ()
{
    modifying_loop(5,6);
    return 0;
}

I compile the it using gcc -o command command.c -lm -lpigpio -L/usr/lib/ which returns me

undefined reference to modifying_loop'

Then to tackle that I link the my_algorithm.c file by using

gcc -o command command.c my_algorithm.c -lm -lpigpio -L/usr/lib/ which gives me the following :

/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here

and same errors for the rest of the variables declared in the header file. Does anyone have any idea what is causing the errors.

HDEV5
  • 17
  • 3
  • What are `someVar1` and `someVar2` used for? You likely need to declare them as `extern` in command.h, and then define them in one of the .c files. But then they are global variables, which is generally a bad idea. – Fred Larson Nov 29 '21 at 21:11
  • 1
    "function is defined in the header file and declared in another c file" --> I think you want "function is _declared_ in the header file and _defined_ in another c file". – chux - Reinstate Monica Nov 29 '21 at 21:12
  • Please update the question title to reflect the real problem which is the "multiple definition" issue as you already have the right way to solve the "undefined reference" issue. – kaylum Nov 29 '21 at 21:13
  • In the .h file, after `void modifying_loop (int a, int b)`, is there a `';'` or `'{"` or what? – chux - Reinstate Monica Nov 29 '21 at 21:13
  • `int someVar1` is not a proper declaration in C because it is missing a semicolon, and so are the following two lines. Edit the question to provide an actual [mre] that contains **exact** source code that can be compiled and linked to reproduce the problem. – Eric Postpischil Nov 29 '21 at 21:14
  • Likely what you need to do is insert `extern` in the declarations in the header: `extern int someVar1;`, and put definitions in one of the source files `int someVar1 = 0;` (which may be shorted to `int someVar1;`, which is technically different, but I will refrain from discussing it here). – Eric Postpischil Nov 29 '21 at 21:15
  • `modifying_loop` also uses `var1` and `var2`, are those supposed to be prepended with `some`? – yano Nov 29 '21 at 21:16
  • 1
    Duplicate of [this](https://stackoverflow.com/questions/69908418/multiple-definition-of-first-defined-here-on-gcc-10-2-1-but-not-gcc-8-3-0/69908511#69908511) and related to [this](https://stackoverflow.com/questions/69462225/global-variables-initialization-in-c-header-file/69464658#69464658), [this](https://stackoverflow.com/questions/1490693/tentative-definitions-in-c-and-linking), and [this](https://stackoverflow.com/questions/64841554/does-variables-declared-in-a-header-file-give-separate-copies-of-the-variables-t/64844348#64844348). – Eric Postpischil Nov 29 '21 at 21:35
  • If you are using GCC 10 or later, you are running into the change of default from `-fcommon` to `-fno-common`. Don't define variables in headers. Declare them in headers (`extern`) and define them once in an appropriate source file. – Jonathan Leffler Nov 29 '21 at 22:17
  • Thank you everyone. Declaring them as extern in the header file and then defining them in RESPECTIVE source files where the variables are used, worked. – HDEV5 Nov 29 '21 at 22:46

1 Answers1

0
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here

you have to make sure, the same code won't be included multiple times. You can do this by embedding the header file code in #ifndef #define #endif macros. These are called include guards

The rest of your code seems to be working fine.

ben
  • 139
  • 5
  • There is no evidence the header is included multiple times in compilation of a single translation unit. The actual problem is likely that the declarations shown are tentative definitions which the compiler treats as full definitions, so the same identifier is defined twice in different translation units. (This is a recent change in GCC behavior, starting with version 10; previously tentative definitions resulted in common symbols to be coalesced instead of regular definitions.) The solution is marking them `extern` and putting regular definitions in one source file. – Eric Postpischil Nov 29 '21 at 21:33
  • @Eric Postpischil, yeah, most of the compilers emit warnings about guards, since that wasn't included in the error list, we don't have any evidence that the user already did not added those. – Rafaelplayerxd YT Nov 29 '21 at 21:41