Thread: gcc linux compiler errors, please help

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up gcc linux compiler errors, please help

    Hi

    I get the following errors when I compile my code in linux gcc.

    /tmp/ccdLcTfo.o: In function `random_in_range':
    topogen.c:(.text+0x0): multiple definition of `random_in_range'
    /tmp/ccPh6Ihg.o:Helper.c:(.text+0xcc2): first defined here
    /tmp/ccdLcTfo.o: In function `main':
    topogen.c:(.text+0x5e): multiple definition of `main'
    /tmp/cc8QlB9O.o:Controller.c:(.text+0x0): first defined here
    /tmp/ccRK4J3x.o: In function `random_in_range':
    trafficgen.c:(.text+0x0): multiple definition of `random_in_range'
    /tmp/ccPh6Ihg.o:Helper.c:(.text+0xcc2): first defined here
    /tmp/ccRK4J3x.o: In function `main':
    trafficgen.c:(.text+0x5e): multiple definition of `main'
    /tmp/cc8QlB9O.o:Controller.c:(.text+0x0): first defined here
    collect2: ld returned 1 exit status

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the code.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > trafficgen.c:(.text+0x5e): multiple definition of `main'
    > /tmp/cc8QlB9O.o:Controller.c:(.text+0x0): first defined here
    trafficgen.c and Controller.c both contain a main()

    Same goes for all the other "multiple definition of ... first defined here" messages.

    One common way of getting into this state is by doing things like
    #include "source.c"
    or by putting code into header files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    insert
    Code:
     int random_in_range(int max)
    {
            long rnd;
            float tmp;
    
            rnd = random();
            if (rnd == RAND_MAX)
                    rnd = random();
            tmp = (float) rnd / (float) RAND_MAX;
            tmp *= max;
            return (int) floor(tmp);
    }
    
    int	main(int argc, char *argv[])
    {
    	FILE *traffic_profile;	
    	FILE *ns_file;
    	FILE *ns_mon_file;
    	int num_flows;
    	int node1, node2;
    	int bandwidth;
    	int total_bandwidth;
    	float start_time;
    	
    	int i, j;
    	
    
    for (i=0;i<num_flows;i++)
    	{
    		do
    		{ 	node1 = random_in_range(num_nodes);
    			node2 = random_in_range(num_nodes);
    		} while (node1 == node2);

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That's not all your code, I assume? What's the command line you use for compiling and linking your code?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Oki - so these are linker errors not compiler errors. I also suspect you may have #included a C file. Don't do that. To access functions from other files you can prototype (declare but not define) them in the relevant file or put the declarations in a shared header. I'd recommend the latter: it's much easier to manage.

    The compiler doesn't care as it just takes 1 source file and spits out 1 object file. The linker is responsible for building a global symbol table out of the input symbols (function names, variable names...), and duplicates aren't allowed (generally - some platforms/architectures/compilers allow it, but it's not to be relied on as it's not portable). You definitely don't want to have more than one main() in your program -- a program should have a single entry point, which is generally main(). If you have 2 separate functions random_in_range with different implementations in different source files, then either give them different names or make them "static" so they're not visible outside the source file they're defined in (so not seen by the linker).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler errors
    By Beowolf in forum C++ Programming
    Replies: 12
    Last Post: 10-31-2007, 09:25 AM
  2. Compiler errors
    By ICool in forum C Programming
    Replies: 9
    Last Post: 10-18-2007, 08:01 AM
  3. Compiler errors
    By ralphwiggam in forum C Programming
    Replies: 1
    Last Post: 09-05-2007, 12:18 AM
  4. Compiler errors
    By JaWiB in forum Windows Programming
    Replies: 3
    Last Post: 03-02-2003, 01:28 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM