2

To teach myself a little C++, I decided to write a little Program to write text to my Saitek X52 Pro joystick display.

I wanted to use Eduards C-library http://plasma.hasenleithner.at/x52pro/

I know I have to place an "extern C" around the methods if I want to use them in my C++ program. But that means changing the Header file of the library - and then it wouldn't build anymore. What would be the correct approach in this case?

EDIT: the suggested method worked partially.

Comm.cpp:

...
extern "C"{
#include <x52pro.h>
}
using namespace std;
int main ( int argc, char *argv[] ) {
    cout<<"entered main"<<endl;
    char *String;
    strcpy(String,"testing");
    struct x52 *hdl = x52_init();
    x52_settext(hdl, 0,String , 7);
    x52_close(hdl);
    return EXIT_SUCCESS;
}

Error Message:

Comm.o: In function `main': 
Comm.cpp|38| undefined reference to `x52_init' 
Comm.cpp|39| undefined reference to `x52_settext' 
Comm.cpp|40| undefined reference to `x52_close'

which are all methods defined in x52pro.h

5
  • defined in x52pro.h or declared in? if declared in, then you need to link to the library that actually contains the definition. what is with all these answers saying to add extern "C"? It's already there!! Commented Jan 18, 2013 at 23:50
  • declared in it. defined in the shared library. Do I need to tell the compiler where to find the shared library? (it had a Makefile which hat an install target though) I've added the extern "C" because of the additions. Commented Jan 19, 2013 at 0:01
  • I think it would benefit your question if you post the relevant parts of the make file. It may be a missing -I parameter. Commented Jan 19, 2013 at 0:14
  • 1
    You will very likely need to tell the compiler which libraries are needed. You may or may not need to specify the location of the library. If you do, how that is done varies on the platform, so you should consult the appropriate documentation for your compiler. Commented Jan 19, 2013 at 0:14
  • I adjusted the makefile accordingly - it was indeed the missing part. Commented Jan 19, 2013 at 10:23

3 Answers 3

4

To use extern "C" in C header files, wrap it so

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

or you can wrap the #includes with extern "C"

extern "C" {
#include <chdr1.h>
#include <chdr2.h>
}

When linking your application, you must tell the linker what library to use and where the library is. From your link, you must add libusb as well. This looks roughly like this

g++ -o app_name Comm.o -L /path/to/library -lx52pro -lusb

When the library is installed in the system lib directory, you can omit the -L /path/... part. If you use a Makefile, you define this in some variables, usually

LDFLAGS = -L /path/to/library
LDLIBS = -lx52pro -lusb

See also Compiling and Linking and Wikipedia - Linker (computing)

Sign up to request clarification or add additional context in comments.

Comments

0

In your C++ code, you can surround the included header file with extern "C" like this:

extern "C" {
#include "c_header_file.h"
}

Then, you would not need to modify the header file of the third party library.

Comments

0
#ifdef __cplusplus
   extern C {
#endif 
    ...

#ifdef __cplusplus
   }
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.