While I am new to C++, (I have experience with Java) I am starting a project which requires the use of GNE, or the Game Networking Engine.
In their tutorial, (granted its from 2003) they use this line:
if ( initGNE( NL_IP, atexit ) ) {
in my code, I use the following:
log("Initializing GNE...");
if(initGNE(NL_IP, atexit)) {
err("GNE failed to initialize!");
err("Press ENTER to close MoServ...");
std::cin.get();
exit(0);
}
log("-> Registering game version...");
setGameInformation(myName(), myVer());
log("-> Initializing console...");
initConsole(false);
setTitle("MoServ");
log("GNE initialized!");
While compiling (with g++ on Linux, via eclipse /w CDT) I get the following errors:
/home/tehtros/Dev/MoServ/Debug/../src/main.cpp:38: undefined reference to `GNE::initGNE(unsigned int, int (*)(void (*)()), int)'
/home/tehtros/Dev/MoServ/Debug/../src/main.cpp:46: undefined reference to `GNE::setGameInformation(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)'
/home/tehtros/Dev/MoServ/Debug/../src/main.cpp:48: undefined reference to `GNE::Console::initConsole(bool)'
/home/tehtros/Dev/MoServ/Debug/../src/main.cpp:49: undefined reference to `GNE::Console::setTitle(char const*)'
Note: In the documentation, they DID say that unless you first call GNE::initGNE(), all other GNE functions would have the “undefinied reference” error.
If I change:
if(initGNE(NL_IP, atexit)) {
to:
if(initGNE(NL_IP, atexit())) {
then I get the error:
../src/main.cpp:38:27: error: too few arguments to function ‘int atexit(void (*)())’
While this is nowhere in the documentation, if I change this:
if(initGNE(NL_IP, atexit())) {
even further, to this:
if(initGNE(NL_IP, atexit(shutdownGNE()))) {
then I get this error:
../src/main.cpp:38:40: error: invalid use of void expression
If I remove the parenthesis, like this:
if(initGNE(NL_IP, atexit(shutdownGNE))) {
then I get this error:
../src/main.cpp:38:39: error: invalid conversion from ‘int’ to ‘int (*)(void (*)())’ [-fpermissive]
/usr/local/include/gnelib/ObjectBrokerClient.h:120:15: error: initializing argument 2 of ‘bool GNE::initGNE(NLenum, int (*)(void (*)()), int)’ [-fpermissive]
As you can see, I’m a little lost. I can’t tell if GNE is broken, if I am just making a small mistake, or if I just have absolutely no clue what I’m doing, and I should just stop trying to program something so big with my little understanding. (then again, I learn by actually doing, not by reading HOW to do.) Could anyone offer any help or support, and clarify what I am doing wrong, if anything?
Edit: I copied and pasted their example directly from their website, without any modifications, and it did not compile.
Solution: I resolved this issue by changing the order of the command. On the command line, I was able to compile like this:
g++ -o test "test.cpp" -lgnelib -lNL
To make this work with eclipse, I added gnelib and NL as libraries, and changed to build command to:
${COMMAND} ${INPUTS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${FLAGS}
I’m not sure why you are playing with the function point argument
atexitbut clearly it won’t work ifinitGNE()expects a pointer to a function pass something different instead. In particular, it won’t work to pass thevoidresult of callingatexit.The errors in your first example simply result from compiling the program but not linking with the appropriate library: you will need to specify the library where the functions are defined. Have a look, e.g., at the installation tutorial which shows that you need to use
-lgnelib.