Thread: Compiling

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    48

    Compiling

    So I've downloaded a genetic algorithm package that was written in C, and I'm calling it from some C++ code, so I need to use g++ instead of cc. However it seems that I can't use the same tags in g++ as in cc because if I try to compile using g++ I end up getting a whole bunch of "undefined reference" to the functions defined in the package.
    Here is the makefile:
    Code:
    # Generated automatically from Makefile.in by configure.                                                                                                                               
    CC          = cc
    PRECFLAGS   = -O
    CPPFLAGS    = -I/home/bbare/pga/include -Dlinux -DWL=32 -DFORTRANUNDERSCORE -DOPTIMIZE -DFAKE_MPI
    FC          = f77
    RM          = /bin/rm -f
    LDFLAGS     = -s  -L/home/bbare/pga/lib/linux  -lpgaO  -lm
    SHELL       = /bin/sh
    
    #    "$@" expands to the target; "$?" expands to the dependency list                                                                                                                   
    CFLAGS      = -o $@ $? $(PRECFLAGS)
    
    LINK.c      = @echo "  Compiling $@" ; $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
    
    default:
            @make classic
            @make dejong
            @make example
            @make maxbit
            @make maxchar
            @make maxint
            @make name
            @make namefull
            @make udtstr
    
    ga_ee: math_utils.cpp calc_subs.cpp EM.cpp EE_utils.cpp In_out.cpp main.cpp
            $(LINK.c)
    
    classic: classic.c
            $(LINK.c)
    
    dejong: dejong.c
            $(LINK.c)
    
    example: example.c
            $(LINK.c)
    
    maxbit: maxbit.c
            $(LINK.c)
    
    maxchar: maxchar.c
            $(LINK.c)
    
    maxint: maxint.c
            $(LINK.c)
    
    name: name.c
            $(LINK.c)
    
    namefull: namefull.c
            $(LINK.c)
    
    udtstr: udtstr.c
            $(LINK.c)
    
    clean:
            @$(RM) classic dejong example maxbit maxchar maxint name namefull \
                   udtstr *.o
    If I put g++ as the compiler (CC variable) it won't even compile the .c files correctly (I get the same undefined reference problems, I've also tried mpiCC (because I think its a parallel program) and I don't know the equivalen c++ compiler, but with mpiCC I still get the undefined references to those functions that I don't get if I just use cc. So I'm quite certain its a flag issue, however I thought that the flags were the same.
    Thanks for the help

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    48
    I forgot to include:
    ga_ee is my project, the rest are all examples included with the package I downloaded

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    This is link time errors not compile time. If an .lib file with headers comes with the package and not source code, you have to pass the lib to g++ to resolve all the external references.
    rwalt

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    48
    Isn't that what the -L flag does?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So I've downloaded a genetic algorithm package that was written in C, and I'm calling it from some C++ code
    Does it's header file have the
    extern "C"
    mechanism to tell the C++ compiler that whatever is in the header file should follow C conventions and not C++ conventions?
    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.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    48
    I'm not sure, what woudl that look like, and how would I add it if it doesn't?

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Is the library pgaO.lib or libpgaO.a ?
    Are the errors telling you that it cannot find an GNU archive ?

    If it's pgaO.lib you may have to change LDFLAGS to

    -s -l/home/bbare/pga/lib/linux/pgaO.lib -lm

    If even that will work or not. The library may have been built with a different compiler/linker.
    Last edited by DarkStar; 08-19-2005 at 02:03 PM.
    rwalt

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    48
    The library is libpgaO.a. The errors are all like this:
    : undefined reference to `PGACreate(int*, char**, int, int, int)'

    and I'm curious as to what Salem was saying. I'm not sure how to check/code what he was talking about

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you look inside most 'C' header files (say stdio.h for example), you should see something like
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    /* bunch of C stuff */
    #ifdef __cplusplus
    }
    #endif
    If your library header file doesn't have this, then you can do this in your C++ source
    Code:
    extern "C" {
    #include <libheaderfile.h>
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Issues
    By pc_doctor in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 10:00 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Problem Compiling
    By Flakster in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 01:09 AM
  4. Compiling
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2005, 01:08 AM
  5. compiling and executing issue with lcc win32
    By GanglyLamb in forum C Programming
    Replies: 10
    Last Post: 12-22-2004, 02:24 PM