logitech mouse logitech mouse - 10 months ago 35
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
, which is included by
. 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


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}

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

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

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

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

Answer Source

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
EXTERN int examplevar;

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

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