Avatar of rich_tanenbaum
rich_tanenbaum
 asked on

Compiler error: multiple definition of `gnu_dev_makedev'

I'm trying to port my app (a function library) over to Oracle Linux. It's a bunch of .c files which are compiled into a bunch of .o files, then I have a small test program which calls into one of the routines. But I get a ton of errors of the form:

multiple definition of `gnu_dev_makedev'

also for `gnu_dev_minor' and `gnu_dev_major'

over and over for functions in many of my .c files. Then it ends with

ld returned 1 exit status

I'm getting spotty info on this online and no good solutions. it sounds like this is some builtin function or macro multiply defined, though I also don't see why it's not a warning instead of an error. I've tried this with cc and gcc and there's no apparent difference.

My compile line for the .o is:

cc -c mycfile.c -I$TOPS -fPIC -shared

(TOPS is where my files are)
 
and for the main program is:

cc testctd.c *.o -lm
 
LinuxCLinux Distributions

Avatar of undefined
Last Comment
rich_tanenbaum

8/22/2022 - Mon
Duncan Roe

It would likely help if you could post some examples.
But in the meantime I  can see one thing - you have -l and -c options used together. That may be all the multiple definition problem is.
You should have:
gcc -c mycfile.c -fPIC
...
gcc -o mylib.so *.o -l$TOPS -shared

Open in new window

Duncan Roe

i.e. separate commands to compile source to object and to build the final product.
-shared is only relevant if you want to build a shred library, so I assumed you wanted to do that.
ELF (i.e. normal) programs are shared anyway - if you run multiple instances
rich_tanenbaum

ASKER
The font that was used obscures what I typed.

My compile line does have a -c which means "only compile, don't link" and what looks like an "l as in Larry" is really "I as in Idaho" to tell it where to find include files. So your gcc example line has no include in it, but i need tohave the include directory on the gcc compile line.

When you ask if I could post some examples, what are you looking for? Source code? gnu_dev_major etc. never appears in my source/
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Duncan Roe

You still don't need -shared - that is a loader directive interpreted by gcc. Put the -I (eye) back by all means.
Post a sample c file that gives the errors and enough headers so it will compile (with errors) submit them using the File link below the Comment box - may be a tar or tgz or whatever EE allows
rich_tanenbaum

ASKER
I think I discovered a workaround. I add -std=c99 to the compile command lines and I no longer get the error.

My test program now runs and gives an answer. But I'm curcious about what you're saying about -shared. My compile line in mymakefile is:

      gcc -c $(TOPS)/toppmain.c -I$(TOPS) -fPIC -shared -std=c99

where $TOPS is the direcotry with .c and .h files

and the main program build line is:

gcc testctd.c -o teststd.out *.o -lm

from a terminal prompt. So I have not created a libxxx.so. The thing that makes me think I'm doing something wrong is that when I change something in a .c file and recompile to a .o file, I need to also rebuild the test program for it to take effect, even though the concept of a shared library would make that unnecessary. Do i need to do something along the lines of

      ld -o libtops.so -dy $(OFILES) $(LIB)

(this is from my makefile when I compiled for Sun about 15 years ago). And then something for the main program build?
 
Duncan Roe

Well done!
In your Makefile, alter the CFLAGS line to include this, or if you don't have one put:

CFLAGS = -std=c99

I think for sure -shared is unnecessary, even if it does no harm.
It sounds to me as though the dependencies in your Makefile are not right. Really, you have to list all the .o files or do something smart if you are sure your program depends on all .o files - I'm thinking of something like in the box (which should get you going, by the way - it's from the Makefile for my editor).
Sun and GNU Makefiles both have smart capabilities but they are in general incompatibly so I'm guessing your still-working Makefile didn't use any.
# Extract from a Makefile I actually use (program name changed to myprog)

SRCS = $(wildcard *.c)
OBJ = $(SRCS:.c=.o)
myprog : $(OBJ)

# The lines below at the end of your Makefile will take care of dependencies automatically -
# any time you change a .h file (in $TOPS), all the appropriate files will be re-compiled

%.d: %.c
        $(CC) -MM -MT $(@:.d=.o) -MT $@ $(CPPFLAGS) $< > $@
-include $(SRCS:.c=.d)

# real tab char before $(CC)

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Duncan Roe

You only need to build a shared library if you have more than one program that will use it. Use gcc rather than ld - gcc will include the necessary system (and gcc) libraries. It would look a bit like:
gcc -o libtops.so -shared *.o

Open in new window

rich_tanenbaum

ASKER
Here is what finally worked for me:

in makefle, one line per c file:

      gcc -c $(TOPS)/toppmain.c -I$(TOPS) -fPIC -shared -std=c99

where TOPS is set as an environment variable to be the path to the directory with the c files.

Then from a terminal, while in the $TOPS directory,

gcc -shared -o libtops.so *.o

to make the .so file, and

gcc testctd.c -o testctd.out -lm $TOPS/libtops.so

to build the test app.

One remianing issue is that i thought sometime during the day I needed to build with -lc along with -lm because of problems linking in calls to the stat function, but at the end of the day it workeed without it, so I',m not sure if it's necessary for my particular code base.
ASKER CERTIFIED SOLUTION
Duncan Roe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rich_tanenbaum

ASKER
I'd give higher grades but I found out the solution to my immediate problem elsewhere.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23