What Is Causing The Undefined Reference In make? - c

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?

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?.

Related

netbeans c program build fail with tdm-gcc msys

i have recently started to use netbeans to program in c. i usually use codeblocks. i have installed tdm-gcc and msys for this purpose.
the programs are getting compiled successfully. but on execution, it shows build failed(exit value 2).
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/f/NetBeansProjects/C_Programs'
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_TDM-Windows/c_programs.exe
make.exe[2]: Entering directory `/f/NetBeansProjects/C_Programs'
mkdir -p build/Debug/MinGW_TDM-Windows
rm -f "build/Debug/MinGW_TDM-Windows/main.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/MinGW_TDM-Windows/main.o.d" -o build/Debug/MinGW_TDM-Windows/main.o main.c
mkdir -p dist/Debug/MinGW_TDM-Windows
gcc -o dist/Debug/MinGW_TDM-Windows/c_programs build/Debug/MinGW_TDM-Windows/main.o build/Debug/MinGW_TDM-Windows/structure_union.o
build/Debug/MinGW_TDM-Windows/structure_union.o: In function `main':
F:\NetBeansProjects\C_Programs/structure_union.c:32: multiple definition of `main'
build/Debug/MinGW_TDM-Windows/main.o:F:\NetBeansProjects\C_Programs/main.c:15: first defined here
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW_TDM-Windows/c_programs.exe] Error 1
make.exe[2]: Leaving directory `/f/NetBeansProjects/C_Programs'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/f/NetBeansProjects/C_Programs'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 657ms)
can somebody help me with what im doing wrong?
You have multiple main() functions in your project:
[…]/structure_union.c:32: multiple definition of `main'
It seems there's one in structure_union and another one in main.c (line 15).
C allows only one definition of main(); remove one of these and it should work.

makefile for a libusb program

I have made a C program using libusb and I am using following command to compile it:
gcc -o usbtest.o usbtest.c -lusb-1.0
The program is working fine. Next, I added the code of "usbtest.c" to kernel module (usbmod.c) and I am stuck with the make file. I am not sure what command I should pass in the "all" section. Here is what I have made:
obj-m := usbmod.o
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
PWD = $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order *-
After running make, I am getting the following error:
anubhav#anubhav-Inspiron-3421:~/Desktop/usb$ make
make -C /lib/modules/3.13.0-46-generic/build SUBDIRS=/home/anubhav/Desktop/usb modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-46-generic'
CC [M] /home/anubhav/Desktop/usb/usbmod.o
/home/anubhav/Desktop/usb/usbmod.c:3:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
make[2]: *** [/home/anubhav/Desktop/usb/usbmod.o] Error 1
make[1]: *** [_module_/home/anubhav/Desktop/usb] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-46-generic'
make: *** [all] Error 2
Not sure if need to bring any header file to my working directory or what. Kindly provide suggestions.
Okay, so this link clarifies my doubts to some extent:
[][1]error: stdio.h: No such file or directory error during make
It says "stdio.h" and all do not exist in kernel space and therefore such errors arise. Besides, obviously my module does not contain any printf so I suppose I don't need "stdio.h".
But it does use libusb extensively. So, is there a way to really create this module.

fatal error: 'endian.h' file not found

Trying to compile C program, every time I run make or gmake as recommended I get this error.
$ make
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C src all
gcc -g -W -Wall -O3 -D_FILE_OFFSET_BITS=64 -D_REVISION=0 -Iinclude -c -o osdep/radiotap/radiotap.o osdep/radiotap/radiotap.c
In file included from osdep/radiotap/radiotap.c:17:
osdep/radiotap/platform.h:6:10: fatal error: 'endian.h' file not found
#include <endian.h>
^
1 error generated.
make[1]: *** [osdep/radiotap/radiotap.o] Error 1
make: *** [all] Error 2
$ gmake
gmake -C src all
gmake[1]: Entering directory '/Users/silent/Desktop/aircr-1.2-rc1/src'
gcc -g -W -Wall -O3 -D_FILE_OFFSET_BITS=64 -D_REVISION=0 -Iinclude -c -o osdep/radiotap/radiotap.o osdep/radiotap/radiotap.c
In file included from osdep/radiotap/radiotap.c:17:
osdep/radiotap/platform.h:6:10: fatal error: 'endian.h' file not found
#include <endian.h>
^
1 error generated.
<builtin>: recipe for target 'osdep/radiotap/radiotap.o' failed
gmake[1]: *** [osdep/radiotap/radiotap.o] Error 1
gmake[1]: Leaving directory '/Users/silent/Desktop/aircr-1.2-rc1/src'
Makefile:25: recipe for target 'all' failed
gmake: *** [all] Error 2
According to some forms online recommended to check the file in this location ~/usr/include/machine but doesn't say what to do if found or not! nothing else was helpful. Then, I found this http://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/Endian.h
silent:~/usr/include/machine
$ ls
_limits.h _types.h fasttrap_isa.h profile.h vmparam.h
_mcontext.h byte_order.h limits.h signal.h
_param.h `endian.h` param.h types.h
As you can the file I am getting the error for is already existed! Any help would be appreciated. Thank you.
PS: I am newbie, I don't know what is this link above talking about :(!
On OSX and iOS, you can include endian.h this way:
#include <machine/endian.h>
But note that this will fail on Android or Linux because they expect #include <endian.h>.
You can also include sys/types.h, which will include the right endian.h both on iOS/OSX and Android/Linux:
#include <sys/types.h>
You have to tell the c compiler where it can find this file:
export CFLAGS="$CFLAGS -I~/usr/include/machine"
then run make.
Alternatively you can edit the file Makefile to add the -I~/usr/include/machine part where necessary.

Axis2/c error in instalaltion

My problem is the following one:
When installing Axis2/c under the guidance of http://petio.org/ws/web_services_page7.html , which seems to be an adjusted manual from the official site I have a problem right after executing the make command in terminal.
At exactly this point:
$ cd /tmp/axis/axis2c-src-1.6.0
$ ./configure --prefix=${AXIS2C_HOME} --enable-libxml2=yes
$ make
I get this error:
XXX#XXXPC:~/Tools/axis2c/axis2c-src-1.5.0$ make
make all-recursive
make[1]: Entering directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0'
Making all in util
make[2]: Entering directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util'
make all-recursive
make[3]: Entering directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util'
Making all in src
make[4]: Entering directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util/src'
Making all in platforms/unix
make[5]: Entering directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util/src/platforms/unix'
/bin/bash ../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../.. -I../../../include -I../../../include/platforms -I../../../include/platforms/unix -g -O2 -D_LARGEFILE64_SOURCE -ansi -Wall -Werror -Wno-implicit-function-declaration -D_GNU_SOURCE -MT uuid_gen_unix.lo -MD -MP -MF .deps/uuid_gen_unix.Tpo -c -o uuid_gen_unix.lo uuid_gen_unix.c
gcc -DHAVE_CONFIG_H -I. -I../../.. -I../../../include -I../../../include/platforms -I../../../include/platforms/unix -g -O2 -D_LARGEFILE64_SOURCE -ansi -Wall -Werror -Wno-implicit-function-declaration -D_GNU_SOURCE -MT uuid_gen_unix.lo -MD -MP -MF .deps/uuid_gen_unix.Tpo -c uuid_gen_unix.c -fPIC -DPIC -o .libs/uuid_gen_unix.o
**uuid_gen_unix.c: In function ‘axutil_uuid_gen_v1’:
uuid_gen_unix.c:62:20: error: variable ‘tv’ set but not used [-Werror=unused-but-set-variable]**
cc1: all warnings being treated as errors
make[5]: *** [uuid_gen_unix.lo] Error 1
make[5]: Leaving directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util/src/platforms/unix'
make[4]: *** [all-recursive] Error 1
make[4]: Leaving directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util/src'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0/util'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/XXX/Tools/axis2c/axis2c-src-1.5.0'
make: *** [all] Error 2
I am not able to understand the reason why this error is being produced.
Because 1.6.0 version is down, I am using the 1.5.0 source version of axis2/c from the official site : http://axis.apache.org/axis2/c/core/download.cgi .
I have additionally installed Apache2 and libxml2, OpenSSL, cURL, libiconv, and zlib.
libraries.
I need your help because I am trying to build a serious project.
Thanks in advance.
Link to mirror of original Axis2/C-1.6.0 source distribution is here.
The main problem is with -Werror flag.
To compile original Axis2/C with modern compiler you must remove -Werror string from build scripts. To do that run this command after unpacking tarball:
find -type f -name configure -exec sed -i '/CFLAGS/s/-Werror//g' {} \;
Then configure and make Axis2/C.
Note: Original Axis2/C-1.6.0 has many issues like memory leaks and crashes. Consider using Axis2/C unofficial project instead. Most critical Axis2/C-1.6.0's issues is fixed within that project.
Also it have additional features https://code.google.com/p/axis2c-unofficial/wiki/IssuesList like JSON support and enhanced CURL-based transport authentication (and some other features).
There is an manual on how to install Axis2/C unofficial on Linux.

C multiple definition error Netbeans

I'm trying to build some native libraries in C that will speed up the monte-carlo simulation for a Java project. I'm doing this on Netbeans 7.0.1, using MINGW
I'm using the mtwist source code of Geof Kuenning (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html) to provide Mersenne Twister pseudorandom numbers.
I have three different models to simulate, and each compiles fine on its own (with output to file rather than using the JNI). However, when I combine the three models under the JNI header file, I start to get a long list of errors when I try to build:
CLEAN SUCCESSFUL (total time: 7s)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory /c/Users/Tom/Documents/LongMemoryModels/longMemModels'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1- Windows/liblongMemModels.dll
make[2]: Entering directory/c/Users/Tom/Documents/LongMemoryModels/longMemModels'
mkdir -p build/Debug/MinGW_1-Windows
rm -f build/Debug/MinGW_1-Windows/longmem.o.d
gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../../../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/longmem.o.d -o build/Debug/MinGW_1-Windows/longmem.o longmem.c
mkdir -p build/Debug/MinGW_1-Windows/_ext/2069311947
rm -f build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o.d
gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../../../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o.d -o build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o ../../../../../cinclude/mtwist/mtwist.c
mkdir -p build/Debug/MinGW_1-Windows/_ext/2069311947
rm -f build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o.d
gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../../../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o.d -o build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o ../../../../../cinclude/mtwist/randistrs.c
mkdir -p dist/Debug/MinGW_1-Windows
gcc.exe -std=c99 -shared -o dist/Debug/MinGW_1-Windows/liblongMemModels.dll build/Debug/MinGW_1-Windows/longmem.o build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o > build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o
build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o: In function mts_lrand':
C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:402: multiple definition ofmts_lrand'
build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:402: first defined here
build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o: In function mts_llrand':
C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:431: multiple definition ofmts_llrand'
build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:431: first defined here
[...]
build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o: In function mts_lrand':
C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:402: multiple definition ofmts_lrand'
build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:402: first defined here
build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o: In function mts_llrand':
C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:431: multiple definition ofmts_llrand'
build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:431: first defined here
[...]
make[2]: Leaving directory /c/Users/Tom/Documents/LongMemoryModels/longMemModels'
make[1]: Leaving directory/c/Users/Tom/Documents/LongMemoryModels/longMemModels'
collect2.exe: error: ld returned 1 exit status
make[2]: * [dist/Debug/MinGW_1-Windows/liblongMemModels.dll] Error 1
make[1]: [.build-conf] Error 2
make: ** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 22s)
(here the elipsis, [...], denotes similar errors that I have omitted)
As you can see, the multiple inclusions seem to relate to the mtwist.h header rather than the JNI header. Moreover, I get the same errors trying to build the JNI project even when mtwist has already been successfully built as a static or dynamic library...
I don't want this question to be too much of an essay, and I'm hoping that error will reveal an obvious error to someone more savvy than me. BUT, I'll post source code too, if this isn't enough to point to a solution.
Please help me resolve this build error if you can.
Tom
EDIT: to make the include structure clearer than in the comments...
In the current arrangement:
[JNI models header].h includes jni.h
mtwist.h includes stdio.h, stdint.h
randistrs.h, includes mtwist.h
[individual model functions].c includes stdio.h stdlib.h math.h mtwist/mtwist.h mtwist/randistrs.h jni.h (unnecessary I suppose)
mtwist.c includes inttypes.h stdio.h stdlib.h sys/timeb.h mtwist.h
randistrs.c includes mtwist.h randistrs.h math.h stdlib.h
All of the .h files have inclusion guards using #ifndef
i guess it's a case of multiple inclusion of prototype.Please try guarding using #ifdef #endif Macro in mtwist.h, for all those prototypes which are giving error.

Resources