[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
(C++ and) Re: gprolog.h
From: |
Timo Karjalainen |
Subject: |
(C++ and) Re: gprolog.h |
Date: |
Mon, 7 Oct 2002 13:22:25 +0300 (EEST) |
On Mon, 7 Oct 2002, eric pramudya wrote:
> Now I'm trying to link the prolog to C++. I have several questions to ask:
>
> 1. Why when I compile It says error in gprolog.h? (It says, error parse
> before template, line 639, I use command g++
> -I$QTDIR/include filename1.cpp filename2.cpp moc_filename1.cpp -L$QTDIR/lib
> -lqt) And I'm using gnu prolog 1.2.8
As someone pointed out a couple of days ago, 'template' is used as an
identifier in gprolog.h. 'template' is a C++ reserved word, so the
compiler rightly refuses to compile the source file that #include's
gprolog.h.
The same guy immediately offered a solution as well: use something like
#define template gp_template
...
#undef template
around gprolog.h. Probably the best technique is to write a separate
header file like this:
----8<----
/* fix_gprolog.h -- a workaround for the 'template' problem in gprolog.h */
#define template gp_template
extern "C" {
#include "gprolog.h"
}
#undef template
----8<----
then #include fix_gprolog.h instead of gprolog.h everywhere in your
program.
That way, you don't have to hack gprolog.h (and you can for example
replace it with a newer version without re-hacking your changes to the new
version), and you also don't have to remember to perform tricks every time
you #include the header.
Finally, if you need to use the identifier that was named 'template' in
gprolog.h, you must use gp_template instead (or whatever you #define
template as).
> 2. How many methods are there in linking prolog to c++? Because some
> resources say that we must build a struct and a kind of
> server in C++ header file. While some people here say that we must put extern
> C surround the interface code.
These are separate issues. GProlog has an API for interfacing to it from
C. To use C linkage in C++ code, you must use extern "C" {} around the
GProlog interface, which was written for C and not C++.
Building structures and servers is what you do with the API; extern "C" {}
is how you get to use the API at all. The extern "C" {} is like "how do I
start this car?" and structs and servers are like "how do I drive from
point A to point B?".
--
Timo Karjalainen "I don't have a life.
I have a program."
- Star Trek: Voyager
- gprolog.h, eric pramudya, 2002/10/07
- (C++ and) Re: gprolog.h,
Timo Karjalainen <=
- Re: gprolog.h, Erick Alphonse, 2002/10/07