I have a C library written by someone else that I wish to call from my C++ program. The C header is structured like this:
#ifndef INC_MOVE_CLIENT_H
#define INC_MOVE_CLIENT_H
#ifdef __cplusplus
extern "C" {
#endif
...
int serverConnect(const char *, const char *, MoveStateDeferred *);
...
#ifdef __cplusplus
}
#endif
#endif // ... INC_MOVE_CLIENT_H
I’m calling serverConnect in my C++ program like so:
#include "helloworld.h"
#include "moveclient.h"
int main(int argc, const char* argv[]) {
const char* ip = "192.168.1.2";
const char* port = "7899";
MoveStateDeferred* m;
serverConnect(ip, port, m);
}
This seems correct to me according to these instructions but when I try to compile I get:
$ gcc helloworld.cpp -o helloworld.out
/tmp/ccuS93Yu.o: In function `main':
helloworld.cpp:(.text+0x3c): undefined reference to `serverConnect'
collect2: ld returned 1 exit status
moveclient.c has the implementation of serverConnect and is in the same directory as the other files. Am I using an incorrect command to compile? Is there something I need to do so that moveclient.c is compiled as well? Or is it something else unrelated to the compile commadn?
The compilation command is wrong.
Typically you do something like this:
…this links all the objects together.