i have a few libraries which I use in modules of the same system.
My problem is that when I do #include to the h files, finally in the system it says "undefined reference to ..." different functions of the module which included twice, once in each different module. It is probably because of the double declaration, how do I manage it ?
I have "rialtor.h" in which:
#include <stdbool.h>
#include "apartment.h"
#include "offer.h"
I have "client.h", in which:
#include <stdbool.h>
#include "apartment.h"
#include "offer.h"
I have "system.c" in which:
#include "rialtor.h"
#include "client.h"
You're looking in the wrong place. An undefined reference is a linker error. You're not including all the needed object files or libraries on the command line that produces your executable.
Related
I have included following headers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
I have also tried to use
#define _GNU_SOURCE
before #include <unistd.h>, but it also does not help.
I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message:
error: ‘F_SETPIPE_SZ’ undeclared (first use in this function)
I actually found out that I don't need this, but I'm just curious why I can't use it.
Thank you.
So here's solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
So here's the solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
You should also pay attention to Chrono Kitsune's other comment.
F_SETPIPE_SZ/F_GETPIPE_SZ are relatively recent. Older kernels (e.g. 2.6.32 as used in RHEL6) don't have them. If you look in /usr/include/linux/fcntl.h and these constants aren't defined, then this API isn't going to work and you'll have to find some way to bypass it in whatever you're building.
I've been having this problem multiple times throughout a program I'm currently working on where gcc doesn't recognize a function or file type that I import from a library even though the include directive is clearly at the top of the program with all the others. To get around this I've just been finding similar libraries and using functions from those, but for what I'm trying to do right now none of the libraries I have found are working. Here is my list of directives:
#include "oscar.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <utime.h>
#include <malloc.h>
I'm currently trying to change the access and modification times in a file. I've tried using utime(), utimes(), futimens() and several other variations and all of them are having the same problem. GCC isn't recognizing many of the functions or file types. For example, I'm trying to use futimens():
futimens(m_file_desc, times);
but gcc gives the following error
warning: implicit declaration of function ‘futimens’
You probably need to need to specify the feature test macros.
From the man page:
futimens():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
So, depending on your glibc version, you need to either define _GNU_SOURCE or _XOPEN_SOURCE, or _POSIX_C_SOURCE with appropriate values.
I have included following headers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
I have also tried to use
#define _GNU_SOURCE
before #include <unistd.h>, but it also does not help.
I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message:
error: ‘F_SETPIPE_SZ’ undeclared (first use in this function)
I actually found out that I don't need this, but I'm just curious why I can't use it.
Thank you.
So here's solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
So here's the solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
You should also pay attention to Chrono Kitsune's other comment.
F_SETPIPE_SZ/F_GETPIPE_SZ are relatively recent. Older kernels (e.g. 2.6.32 as used in RHEL6) don't have them. If you look in /usr/include/linux/fcntl.h and these constants aren't defined, then this API isn't going to work and you'll have to find some way to bypass it in whatever you're building.
I've created a classic client server program in C on my Windows machine in Eclipse. The code itself doesn't generate any errors. The only error I do get is concerning the libraries. I have included the following libraries:
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
But there is an error that appears when I try to build the project:
error "ws2tcpip.h is not compatible with winsock.h. Include winsock2.h instead".
The only issue is that I have not included winsock.h and included only winsock2.h just as they specified.
How do I solve this issue?
UPDATE
I've now defined the libraries in the following order:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
But now I get more errors:
undefined reference to `socket#12'
undefined reference to `gethostbyname#4'
undefined reference to `connect#12'
undefined reference to `htons#4'
undefined reference to `WSACleanup#0'
undefined reference to `closesocket#4'
undefined reference to `WSACleanup#0'
undefined reference to `WSAStartup#8'
UPDATE 2
I think maybe this warning might have something to do with the errors I still keep getting:
ignoring #pragma comment [-Wunknown-pragmas]
winsock2.h has a line at the top:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
You need to include winsock2.h before windows.h.
I have included the following libraries into my code.
#include <minix/drivers.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include <string.h>
Now I get the following error:
In file included from /usr/local/include/curl/curlbuild.h:152
from /usr/local/include/curl/curl.h:34
from xxx.c:2
/usr/pkg/gcc44/lib/gcc/i686-pc-minix/4.4.3/include-fixed/sys/socket.h:134: error: conflicting types for '_send'
/usr/include/minix/ipc.h:152: note: previous declaration was here
As far as I know this means that _send has been declared in two libraries (minix/drivers.h and curl/curl.h), I was wondering if it is possible to resolve this problem or work around it in some way?
Since you're on minix, you can modify one (or both) of the libraries with objcopy. From the man page:
--redefine-sym old=new
Change the name of a symbol old, to new. This can be useful when
one is trying link two things together for which you have no source,
and there are name collisions.
or, if you don't need _send from one of the libraries:
-L symbolname
--localize-symbol=symbolname
Make symbol symbolname local to the file, so that it is not visible
externally. This option may be given more than once.
Of course, you will need to update your headers accordingly. I would also recommend naming the modified library and headers something else so it's clear that you've modified them.