-
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
-
I forgot to include:
ga_ee is my project, the rest are all examples included with the package I downloaded
-
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.
-
Isn't that what the -L flag does?
-
> 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?
-
I'm not sure, what woudl that look like, and how would I add it if it doesn't?
-
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.
-
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
-
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>
}