Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C Headers in C++

I have searched the googles for this and have found that you use

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

To include a C library inside of a C++ library... however, when I do this. The C++ program seems to pick up all my #defines and struct definitions but none of the function declarations leaving me with undefined reference to `function'.

Here is a minimal amount of src I am using.

json.h

//json.h
typedef struct json_object json_object;
struct json_object {
    char key[15][50];
    int size;
    char value[15][50];
};
void json_parseText(char * text, struct json_object *jo);

test.cpp

//test.cpp
extern "C" {
    #include "json.h"
}

int main() {
    struct json_object jo;
    char * keyVal;
    char * text = "{ \"MsgType\": \"article\" }";

    json_parseText(text, &jo);
}

g++ yields the following:

test.cpp:(.text+0x2c): undefined reference to `json_parseText'

notice that it is not complaining about the struct definition, so it seems like it got that from the header file. But not the function. This baffles me. I have never used C++ before now, but for my testing framework it must be in C++. Let me know if you have any thoughts on how to fix this. Thanks.

like image 214
Steven Avatar asked Aug 19 '11 21:08

Steven


People also ask

Can you use header files in C?

C language has numerous libraries that include predefined functions to make programming easier. In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”.

How do headers work in C?

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ' #include '. Header files serve two purposes.

What are headers in C?

In C language, header files contain the set of predefined standard library functions. The “#include” preprocessing directive is used to include the header files with “. h” extension in the program. Here is the table that displays some of the header files in C language, Sr.No.

Do I need to include the header in the C file?

c') should include them itself, and not rely on its header to include them. The header should only include what users of the software need; not what the implementers need.


1 Answers

That's a link-time error. In other words, your C++ compiler picked your header all right; you just forgot to link with your library.

Right now, you tell your compiler that such functions and structures exist, but not where it can find them.

For a shared library (.so), you'll have to pass -l[lib name] to G++; you might also have to specify additional folders in the library search path, as -l requires a file name (without the extension) instead of a path. For a static library (.a), you'll have to include its path in the files to compile.

like image 50
zneak Avatar answered Nov 07 '22 04:11

zneak