Libraries

Are there any good pointers to how to create libraries correctly that can be linked in C++ applications?

I've written a library that I want to access from C++ now. The library was written in pure C. Possibly the way I create the objects may be wrong.

1
2
3
4
5
6
[root@<DEVEL> ops]# g++ -o ver ./ver.c libRemoteNode/rn_version.o
/tmp/ccPm7utk.o: In function `main':
ver.c:(.text+0x12): undefined reference to `rn_version()'
collect2: ld returned 1 exit status
[root@<DEVEL> ops]# gcc -o ver ./ver.c libRemoteNode/rn_version.o
[root@<DEVEL> ops]#  


ver.c is a simply C program that tests the linking.

1
2
3
4
5
#include "libRemoteNode/RemoteNode.h"

int main(int argc, char **argv) {
        printf("%s\n", rn_version());
}


When + use g++ to link I get underfined reference but when I use gcc to link it works well. Something must be amiss as to how I compiled those objects

1
2
3
gcc -c -O3 -Wall -DOPS -DNDEBUG -fPIC -g -I. -I../../mxml-2.5   -DVERSION= -DDATA=\"Development\" \
                -DBDATE=\"`date +%m%d%y-%H%M%S`\" -DVERSION_MAJOR=0 \
                -DVERSION_MINOR=0 -DVERSION_SUB=1 rn_version.c 


Thanks,
Chris
I've figured it out but am not sure if this is "the right way".

In my RemoteNode.h (C) file I have this definition

 
extern char * rn_version(void);


That works great for C.

In ver.cc I have removed the reference to RemoteNode.h and have added this to the program:

 
extern "C" char * rn_version(void);


I guess I should create a special RemoteNode.hh file for the C++ programs to use instead of RemoteNode.h

Yes, you got it. The typical pattern is:

1
2
3
4
5
6
7
8
9
#ifdef __cplusplus
extern "C" {
#endif

/* Rest of header file here */

#ifdef __cplusplus
}  /* close extern "C" block */
#endif 

Very nice. Here is how I did it to keep my RemoteNode.h file from having C++ stuff in it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef _REMOTENODE_HH_
#define _REMOTENODE_HH_ 1

#ifdef __cplusplus
extern "C" {
#endif

// bring in standard library definitions.
#include "RemoteNode.h"

#ifdef __cplusplus
}  

// RN Class definition
class RN {
        private:
        public:
                char * version(void);
                RN();
                ~RN();
};
#endif 

#endif

// vi: set ts=2 sw=2:// 
Topic archived. No new replies allowed.