Unable to locate problem in code/makefile--> clang: error: linker command failed with exit code 1

262 Views Asked by At

Obtaining a clang: error: linker command failed with exit code 1 when running makefile. Specifically when trying to create an executable:

gcc powers.c -Wall -pedantic -ansi -c
gcc bounds.o powers.o -o pracTwo
duplicate symbol '_power' in:
    bounds.o
    powers.o

Makefile:

pracTwo: bounds.o powers.o
    gcc bounds.o powers.o -o pracTwo

bounds.o : bounds.c macros.h
    gcc bounds.c -Wall -pedantic -ansi -c

powers.o : powers.c
    gcc powers.c -Wall -pedantic -ansi -c

clean: 
    rm -f pracTwo bounds.o powers.o

Expected result, 2 .o files created (bounds.o and powers.o) then they are linked together to make a pracTwo executable. Tried looking through the my files however unable to locate the problem?

Here are the headers of the .c files: bounds.c

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

void intCheck();
void doubleCheck();
void charCheck();

And powers.c

#include <stdio.h>
#include <stdlib.h>

int power();

And here is the header file:

#ifndef MACROS_H
#define MACROS_H

#include "powers.c"
#define FALSE 0
#define TRUE !FALSE

#define BETWEEN (n<lower) || (n>upper)

#endif

Unsure what the issue is, cannot locate any error of the code. Unsure what duplicate symbol '_power' in: means.

1

There are 1 best solutions below

0
Allan Wind On

When you include powers.c in bounds.c and link both bounds.o and power.o you end up with the same symbol linked twice. The code only includes declaration so I was not able to reproduce the error as my gcc complained about power not _power on my system.

I suggest you don't include the .c file and rely on just linking (power.o). You may also be able to declare the function static to make it file local.

Unrelated, identifiers that start with _ are reserved (7.1.3) so it's advisable that you don't use that in your code.

BETWEEN should probably be:

#define BETWEEN (n<lower) && (n>upper)

and you may want to use a function-like macro instead with the arguments passed in:

#define BETWEEN(lower, n, upper) ((n)<(lower)) && ((n)>(upper))