We help IT Professionals succeed at work.

Compiler error: multiple definition of `gnu_dev_makedev'

1,631 Views
Last Modified: 2012-05-10
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
 
Comment
Watch Question

Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
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 RoeSoftware Developer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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/
Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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 RoeSoftware Developer
CERTIFIED EXPERT

Commented:
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

Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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.
Software Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I'd give higher grades but I found out the solution to my immediate problem elsewhere.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.