logitech mouse logitech mouse - 1 month ago 7
C Question

`multiple definition of` lots of variables error - is my make file incorrect?

I am getting an error when I compile

multiple definition of
lots of variables. For example:

/tmp/ccwHwJ7t.o:(.data+0x0): multiple definition of `serial_number'
/tmp/ccmT1XNI.o:(.data+0x0): first defined here


All the variables are located in
ftdi.h
, which is included by
main.c
. Is there something wrong with my make file that is causing this to be included twice? or am I looking in the wrong directio.

SSHELL = /bin/sh
CC = gcc

APP = npi_usb_ftdi
INC = include

INCDIRS +=-I${INC}

CFLAGS= ${INCDIRS} -Wall -Wextra
LIBS = libftd2xx.a -ldl -lpthread -lrt


all: ${APP}

${APP}: src/main.c src/ftdi.c src/vt100.c src/monitor.c
${CC} ${CFLAGS} src/main.c src/ftdi.c src/vt100.c src/monitor.c -o ${APP} ${LIBS}

ftdi.o:
${CC} -c -o src/ftdi.o src/ftdi.c

vt100.o:
${CC} -c -o src/vt100.o src/vt100.c

monitor.o:
${CC} -c -o src/monitor.o src/monitor.c


clean:
rm -f src/*.o ; rm -f src/*~ ; rm -f *~ ; rm -f ${APP}

Answer

You probably include the .h file in other source files too. No problem, but only in one source file should the variables be declared and in the others just defined. I use:

// ftdi.h
#ifndef EXTERN
# define EXTERN extern
#endif
EXTERN int examplevar;

// main.c
#define EXTERN
#include "ftdi.h"

// ftdi.c
#include "ftdi.h"