Thread: multiple definition of functions

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Smile multiple definition of functions

    Hi,
    I'm working in a project with multiples c files and I have a file called common.h (a common header file) and a file called common.c for functions.
    I have some functions declared in common.c that are used by all the files in the project. Every file have the lines #include "common.c" and #include "common.h" on the beginning.
    When I compile with gcc under Linux I get the following error for each function in common.c:
    Code:
    common.c:(.text+0x0): multiple definition of `<function name>'
    /tmp/ccgDKaJP.o:triviald.c:(.text+0x0): first defined here
    Is there any specific way to define functions in order to get rid of this error?
    Which file would you declare a common typedef for all the files in my project (common.h and common.c)?

    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JOCAAN
    Every file have the lines #include "common.c" and #include "common.h" on the beginning.
    You should not be including a source file, i.e., don't #include "common.c".

    Quote Originally Posted by JOCAAN
    I have some functions declared in common.c that are used by all the files in the project.
    Keep the definition (implementation) of those functions in common.c and forward declare them in common.h instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You should not include .c file.
    Perhaps you meant to link object file?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You never include a .c file. Basically, the preprocessor sticks the file contents right in where the #include statement was, so yes, you have that function defined in every file that #includes common.c.

    You should only include the header (.h) files, which generally only have function prototypes, type definitions and constant definitions. Every .c file is compiled separately into object code, then at the end, you link all the object files together. You should also make sure your headers use include guards.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    I've removed the #include "common.c" in my files and I've add #include "common.c" in "common.h".
    When I compile I get a lot of errors:
    Code:
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3,
                     from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from common.h:2,
                     from common.c:3:
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    common.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    /usr/include/unistd.h:288: error: old-style parameter declarations in prototyped function definition
    common.c:77: error: expected ‘{’ at end of input
    make: *** [tftp] Error 1
    Here's my makefile:
    Code:
    CC=gcc
    
    all: tftp
    
    tftp:
    	$(CC) triviald.c common.c -Wall -o triviald
    	$(CC) trivial.c common.c -Wall -o trivial
    clean:
    	rm triviald trivial
    Any ideas?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think, somehow, you missed the message in all three of our responses. NEVER INCLUDE A .c FILE, ANYWHERE! Not even in it's corresponding .h file.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    so... ok, but how can I manage to use the functions declared in common.h?

    ... i mean in common.c

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    CC=gcc
    
    all: tftp
    
    tftp:
    	$(CC) triviald.c common.c -Wall -o triviald
    	$(CC) trivial.c common.c -Wall -o trivial
    clean:
    	rm triviald trivial
    See how there are two .c files specified in each compile command? That makes the actual functions in common.c available to any other .c files in the same compile command.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    I've solved it!
    The problem was I needed to put functions prototypes in common.h

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JOCAAN
    The problem was I needed to put functions prototypes in common.h
    That is what I mean by:
    Quote Originally Posted by laserlight
    Keep the definition (implementation) of those functions in common.c and forward declare them in common.h instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  2. DevCpp error: multiple definition of `img'
    By TriKri in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2007, 08:11 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 4
    Last Post: 03-11-2005, 05:45 PM