Why is GCC not seeing functions in header file? - header-files

When I try to compile I get this:
aero$ gcc hostinfo.c
/tmp/cc2RfYB2.o: In function `main':
hostinfo.c:(.text+0x72): undefined reference to `Gethostbyaddr'
hostinfo.c:(.text+0x8b): undefined reference to `Gethostbyname'
collect2: ld returned 1 exit status
hostinfo.c looks like this:
/* $begin hostinfo */
#include "csapp.h"
int main(int argc, char **argv)
{
...
if (inet_aton(argv[1], &addr) != 0)
hostp = Gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
else
hostp = Gethostbyname(argv[1]);
...
}
/* $end hostinfo */
And csapp.h looks like this:
/* $begin csapp.h */
#ifndef __CSAPP_H__
#define __CSAPP_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
...
/* DNS wrappers */
struct hostent *Gethostbyname(const char *name);
struct hostent *Gethostbyaddr(const char *addr, int len, int type);
...
#endif /* __CSAPP_H__ */
/* $end csapp.h */
Both hostinfo.c and csapp.h are in the same directory. I'm new to Unix and gcc so I'm sure it's something simple.

You need to compile the file containing the implementation of these functions, and then link it with the calling file:
gcc -c csapp.c # This creates csapp.o
gcc -c hostinfo.c # This creates hostinfo.o
gcc -o hostinfo hostinfo.o csapp.o # Link them together, creating executable hostinfo

I was basically having the error of undefined reference to `function_name'
It was finally resolved by combining both the c files in the executable
For example, I had csv.c and columns.c with columns.h, so instead of
gcc csv.c -o csv
I used
gcc csv.c columns.c -o csv

Related

"DRM_IOCTL_MODE_GETRESOURCES" not equal to "0xA0" as defined in drm.h

I'm trying to use ioctl() in C++ under Linux to initialize KMS/DRM, but I'm facing some issues with DRM_IOCTL_MODE_GETRESOURCES. This macro is defined as follows in /usr/include/libdrm/drm.h:
#define DRM_IOCTL_MODE_GETRESOURCES DRM_IOWR(0xA0, struct drm_mode_card_res)
However, when I try to replace DRM_IOCTL_MODE_GETRESOURCES by "0xa0" or "0xA0", I don't get the same output!
This is my code:
#include <libdrm/drm_mode.h>
#include <libdrm/drm.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
int main()
{
int dri_fd = open("/dev/dri/card0", O_RDWR);
struct drm_mode_card_res res;
ioctl(dri_fd, DRM_IOCTL_SET_MASTER, 0);
// This is the problematic one:
ioctl(dri_fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
printf("VALUE: %d", res.count_connectors);
return(0);
}
This outputs: VALUE= 4
If I change "DRM_IOCTL_MODE_GETRESOURCES" by "0xa0" or "0xA0", it outputs: VALUE = 0, which is very strange to me.
(You may ask why I'm not using the defined macro, the thing is I'm trying to write this code in x86_64 assembly, so I need the arguments for the syscall! :( )
I'm using g++ but I've tried the same syscall with assembly (nasm) and I got the same result (value = 0)
Thanks a lot for your help!

Fatal error when compiling #include <sys/uio.h> on project [windows]

trying to compile a file for class, using the mingw compiler on windows 10. Compiling with g++ gives me an error stating
\projectFile.o
mingw32-g++.exe -o D:\GitHub\GitRepo\projectFile.exe D:\GitHub\GitRepo\projectFile.o
D:\GitRepo\projectFile.cpp:16:20: fatal error: sys/uio.h: No such file or directory
compilation terminated.
From what ive read this header file
#include <sys/uio.h>
is a unix header and is generally included with most unix build environments. I am working on Windows 10 build and have been unsuccessful in trying to get this to work. Is there a work around for windows using different headers? Is there a while to install this file somehow?
The project is a generalized XML parser that as a student my job is to extract functions from the main file so that they can be reused (OOP design space)
#include <iostream>
#include <iterator>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/io.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <algorithm>
#include <ctype.h>
#include "XMLParser.hpp"
Built on Windows 10 (lastest build) with Mingw-64 (lastest version)
This will not compile for me
This fixed my problem creating this uio.h file in sys directory of mingw64
#ifndef SYS_UIO_H
#define SYS_UIO_H
#include <inttypes.h>
#include <unistd.h>
struct iovec
{
void *iov_base; /* Base address of a memory region for input or output */
size_t iov_len; /* The size of the memory pointed to by iov_base */
};
ssize_t readv(int fildes, const struct iovec *iov, int iovcnt);
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt);
#endif /* SYS_UIO_H */

Compilation errors with socket bind function

At the very beginning I include the following files and everything goes fine with the bind function
#include "stdafx.h"
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
bind function
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
Things start messing up when I include the mysql libraries as well
#include "stdafx.h"
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
//mysql connections
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
I started debug and it give me this error
IntelliSense: no suitable conversion function from "std::_Bind<false, void, SOCKET &, sockaddr *&, int>" to "int" exists
error C2440: '=' : cannot convert from 'std::_Bind<false,void,SOCKET &,sockaddr *&,int>' to 'int'
Please help.
I guess the problem is not in including the Mysql library, i got this problem once and i fixed it removing the
using namespace std;
or another solution is to call bind from the global namespace to avoid this kind of problems;
i forgot to write that calling bind from the global namespace is done like this :
::bind(...);
Hope this helps.

c++ error: ??? does not name a type

Okay upon running g++ main.cpp -o services
If i do all this in one class lets say .cpp it works fine, but whenever i divide it into another class i keep getting errors, and i really do not understand why, all i did was move the code to another file, and included it.
I am thrown:
[Admin#shadowrealm ircservices]$ g++ main.cpp -o services
In file included from services.cpp:1:0,
from main.cpp:4:
services.h:23:2: error: âSOCKETâ does not name a type
services.h:24:2: error: âHOSTENTâ does not name a type
services.h:25:2: error: âSOCKADDR_INâ does not name a type
services.h:
#ifndef SERVICES_H
#define SERVICES_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
class services {
public:
services(); //perhaps init something important here, dunno
~services();
int connect();
private:
SOCKET sock;
HOSTENT* host;
SOCKADDR_IN address;
};
#endif /* SERVICES_H */
services.cpp:
#include "services.h"
services::services()
{
//do nothing
}
services::~services()
{
//TODO: incase crash, log why.
}
int services::connect()
{
return 0;
}
C++ is case sensitive.
The right is
sockaddr_in
hostent
If I am not mistaken SOCKET or socket does not name a type at all.

why does it matter if the #include are in the .h file or in .cpp file?

I have a cpp file contains this include:
#include "twitServer.h"
and in twitServer.h I have:
#ifndef twitServer_twitServer_h
#define twitServer_twitServer_h
#include <string>
#include <map>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <netdb.h>
#include <errno.h>
#include <sstream>
#include <algorithm>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "twitUser.h"
using namespace std;
void startServer(string port);
#endif
But the Xcode says for this line:
if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) {
fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
exit(1);
}
that the getaddrinfo is not defined... why that?
if the includes are in the cpp file it works fine how comes
Not sure how to fix your problem, but by convention, you should never include in the header file, because you might write code some day, needing your header file, but not all the includes.
Try including Ws2tcpip.h and see if that solves the problem. This is what MSDN has to say about it (see the requirements section)
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738520%28v=vs.85%29.aspx
Also, let me say that this seems like an awful lot of header files included and some should not be matched unless you really know what you are doing. There are quite a bit of C++ headers (iostream (which is included twice by the way), cstdio, cstdlib, algorithm, list, string, ctime...) mixed with C headers (stdio.h, stdlib.h string.h). Maybe it's time for some late spring cleaning in there :)