Problem with makefiles

This is a discussion on Problem with makefiles within the Tech Board forums, part of the Community Boards category; I get a problem(a global std::list from token.h seems to be defined(not declared) at multiple places) similar to what happens ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Problem with makefiles

    I get a problem(a global std::list from token.h seems to be defined(not declared) at multiple places) similar to what happens in Circular Header includes, but I'm convinced it is because of the makefile I wrote:
    Code:
    test: token.o lexer.o testmain.o
        g++ token.o lexer.o testmain.o -std=c++0x -g -o test
        # Executing 'test' ...
        ./test
        
    token.o : token.cpp token.h
        g++ token.cpp -c -g -std=c++0x -o token.o
    
    lexer.o : lexer.cpp lexer.h
        g++ lexer.cpp -c -g -std=c++0x -o lexer.o
    
    testmain.o : test_main.cpp
        g++ test_main.cpp -c -g -std=c++0x -o testmain.o
    The reason I'm convinced is that it does not happen with
    a
    Code:
    g++ test_main.cpp lexer.cpp token.cpp -std=c++0x
    Any insight ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Check the header files. See if there is a definition in there, and if they're missing #include guards.

    I don't think it's Make that's the problem. It might be how g++ handles linking several object files versus compiling several source files. When you compile a single source file to an object file, the compiler doesn't necessarily know what you're going to do with that object file, so it leaves everything in there as you requested. But if you give all 3 source files at once, the compiler may be "smart" and say "ah! these 3 global variables are the same, so I'll merge them".

    I also noticed that your manual command doesn't specify the -g flag, which, while extremely unlikely, may be playing a part.

    Can I assume that if you run all of the commands exactly as they are in the make file by hand that you get the same error? We can rule out the make utility that way.

    Also, I won't have a Linux system available until tomorrow ~8am (California time), but if none of the above helps, posting some/all of the code may be helpful.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I think I've managed to reproduce the problem with the following files, though I think I'm missing something very foolish here..!

    makefile
    Code:
    main: test1.o main.o
        g++ test1.o main.o -std=c++0x -g -o main
    
    test1.o: test1.cpp test1.h
        g++ test1.cpp -c -g -std=c++0x -o test1.o
    
    main.o : main.cpp
        g++ main.cpp -c -g -std=c++0x -o main.o
    main.cpp
    Code:
    #include "test2.h"
    #include "test1.h"
    using namespace std;
    int main()
    {
        std::list<int>::iterator lit;
        lit = mylist.begin();
        foo();
        return 0;
    }
    test1.cpp
    Code:
    #include "test2.h"
    void foo()
    {
    }
    test1.h
    Code:
    #ifndef TEST1
    #define TEST1
    void foo();
    #endif
    test2.h
    Code:
    #ifndef TEST2
    #define TEST2
    #include<list>
    std::list<int> mylist = {1,2,3,5,6,7,0};
    #endif
    The error I get is:
    Quote Originally Posted by make
    main.o: In function `main':
    /home/manasij7479/test/main.cpp:5: multiple definition of `mylist'
    test1.o:/home/manasij7479/test/test1.cpp:3: first defined here
    collect2: ld returned 1 exit status
    make: *** [main] Error 1
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    > std::list<int> mylist = ...;
    This should be in ONE .cpp file only.

    The .h file should have
    extern std::list<int> mylist;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Salem View Post
    > std::list<int> mylist = ...;
    This should be in ONE .cpp file only.
    Just for clarification... Should it be outside all function code ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    > Just for clarification... Should it be outside all function code ?
    What do you think?
    What have you tried?

    You could try both, just to see what happens
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I did both...
    Both works..but the "inside function way" requires the function ..say void init() to be called first .
    What I was asking : Is there any sort of undefined behaviour or something associated with the other approach ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    I'm not a C++ person at all, but I don't see anything wrong with "the other approach", other than using a global variable. If it's outside all function code and class definitions, it's just a global variable. You only want a global variable defined in one place. You want the extern declaration visible (via a header file) in all the other .cpp files that need access to that global symbol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefiles
    By gar35 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2010, 08:35 AM
  2. Makefiles!
    By AmbliKai in forum C Programming
    Replies: 6
    Last Post: 11-29-2007, 01:36 AM
  3. Makefiles?
    By Granger9 in forum C Programming
    Replies: 1
    Last Post: 08-17-2002, 05:43 AM
  4. makefiles
    By hankspears in forum C Programming
    Replies: 13
    Last Post: 04-26-2002, 09:42 AM
  5. Makefiles
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 09:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21