0

The following make output is showing an undefined reference, and I am not sure what is causing it. Could someone help?

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe
make[2]: Entering directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/main.o.d
gcc -std=c99   -c -g -I../mongodb-mongo-c-driver/src/\*.c -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/main.o.d -o build/Debug/Cygwin_4.x-Windows/main.o main.c
mkdir -p dist/Debug/Cygwin_4.x-Windows
gcc -std=c99    -o dist/Debug/Cygwin_4.x-Windows/mongodrivertest build/Debug/Cygwin_4.x-Windows/main.o  
nbproject/Makefile-Debug.mk:61: recipe for target `dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe' failed
make[2]: Leaving directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
nbproject/Makefile-Debug.mk:58: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/g/workspace/c_cpp/MongoDriverTest/main.c:19: undefined reference to `_mongo_connect'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

And this is the content of my main.c:

#include <stdio.h>
#include <stdlib.h>
#include "../mongodb-mongo-c-driver/src/mongo.h"

int main(int argc, char** argv) {
    int status;
    mongo conn[1];

    status=mongo_connect(conn, "127.0.0.1", 27017);
    return EXIT_SUCCESS;
}  

It was working two days ago, I reinstalled the OS and now it's not working anymore and I don't seem to find the cause. mongo.h exists, mongo.o is there as well. mongo_connect is in mongo.c. Any idea?

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Andrew
  • 6,254
  • 16
  • 59
  • 93
  • Are you actually linking in mongo.o? It doesn't seem like it. Perhaps you were dependent on an environment variable that was lost when you reinstalled your OS? – jamesdlin May 20 '12 at 19:36

1 Answers1

4

Your link line is:

gcc -std=c99 -o dist/Debug/Cygwin_4.x-Windows/mongodrivertest build/Debug/Cygwin_4.x-Windows/main.o  

It doesn't tell GCC where to collect mongo_connect() from. You need to specify the Mongo library on the command line.

Given the include line in your source code:

#include "../mongodb-mongo-c-driver/src/mongo.h"

You might add options:

-L../mongodb-mongo-c-driver/lib -lmongo

to the link line. Both the location and the library name are guesses. That would pick up libmongo.dll or libmongo.lib from the specified directory.

If you can't find the library under the ../mongodb-mongo-c-driver directory, somewhere, you may have to build and install it. Alternatively, it may already be installed and you simply need to ensure you are referencing the correct locations where it is installed.


Also, as a general rule, avoid pathnames like that in the source code. You should specify:

#include "mongo.h"

and supply a compile line option to specify where to look for it:

-I../mongodb-mongo-c-driver/src

See also: What are the benefits of a relative path such as #include "../include/header.h" for a header?.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I'm using Netbeans if you have custom advice – Andrew May 20 '12 at 19:42
  • I don't use NetBeans; I can't give any meaningful advice. – Jonathan Leffler May 20 '12 at 19:43
  • the directory mongo-db-mongo-c-driver is there. I can click on it from Netbeans and it opens the mongo.h file, so it must be there – Andrew May 20 '12 at 19:44
  • The header is there; that's undisputed. You'd be getting different errors if it were not. But the header is 'merely' a declaration of what's in the library, not the actual library code. (The header is crucial to safe use of Mongo at compile time, but the library is also crucial.) You need to collect the library so that you have the definitions of the functions too. – Jonathan Leffler May 20 '12 at 19:46
  • thanks :) I solved it somehow. I added ../mongodb-mongo-c-driver/src/*.c after -std=c99, now it uses it during compilation. I had it before at include directories but seemingly it didn't use it. Now it works – Andrew May 20 '12 at 19:54
  • Well, yes...that is one way to do it, but it is not the recommended way. It means that you recompile the source for the driver each time you make a change to your source file. This is the sort of thing that libraries prevent you from having to do. I recommend building the driver library, and then using it. However, compute cycles are cheap these days and what works for you is OK. You're lucky there are no test programs or other extraneous C files in the Mongo source directory to confuse you. – Jonathan Leffler May 20 '12 at 19:58