Hi,
this is my problem: I have two file in C,one of this have .c extenction and I have compile it with gcc to obtain an object file .This file explain what function (included in another file)do. the other one have .h extection,and contain jast the name of some function that are explain in the previous file, with .c extenction. so, this two file contain some libraries that a C++ file can't read. At this point I have wrote a c++ file that include the file with .h extention in order to use the function defined with c file, and the idea is that this file .h is see like a library. I have complile this c++ file with g++. now I have to link the object file obtain by C file with the object file obtain with C++ file. how can I do this?
thanks!
I have done as you tell me...but the problem still remain. in the c++ file I have write(myprogram.c)
#include<.....>
#include<....>
extern "C" {
#include "file.h"
}
int main(){
here I have use the function explain in C file
}
then I have compile it with g++.
I have write the .h file in this way
#ifdef _cplusplus
extern "C" {
#endif
struct indirizzo{
unsigned int ips;
unsigned int ipd;
unsigned int sPort;
unsigned int dPort;
unsigned int protocol;
unsigned int fid;
unsigned int ipsmasks;
unsigned int ipdmaskd;
};
int ValidateFunc(const char *buffer, int length, int socket);
struct indirizzo * parseDoc(char* buffer, int dim);
const char *XmlwriterMemory1(const char *file);
const char *XmlwriterMemory2(const char *file);
#ifdef _cplusplus
}
# endif
this is my file.h
in the last C file(myfile.c) I have explain what this function do.
then I have compile this with gcc.
now I write on terminal
g++ -o myfile.o myprogram.o
the terminal still say that it don't find function parseDoc(), XmlwriterMemory1(), XmlwriterMemory2() and ValidataFunc() in the main()
sorry! but it don't work. if I do like you say, I have new errors.it don't recognize the library (and the variable )of the C program, the one in which I have explain what function do.... I must modify something in the .c file?
I'll try to explain batter my problem:
I have 3 file. 2 are write in C and one in C++. one in C have .h extension and it is (myfile.h)
#ifdef _cplusplus
extern "C" {
#endif
struct indirizzo{
unsigned int ips;
unsigned int ipd;
unsigned int sPort;
unsigned int dPort;
unsigned int protocol;
unsigned int fid;
unsigned int ipsmasks;
unsigned int ipdmaskd;
};
int ValidateFunc(const char *buffer, int length, int socket);
struct indirizzo * parseDoc(char* buffer, int dim);
const char *XmlwriterMemory1(const char *file);
const char *XmlwriterMemory2(const char *file);
#ifdef _cplusplus
}
# endif
the other one have .c extension and the form is (myfile.c)
class EEE {
public:
unsigned int ips;
unsigned int ipd;
unsigned int sPort;
unsigned int dPort;
unsigned int protocol;
unsigned int fid;
unsigned int ipsmasks;
unsigned int ipdmaskd;
now I compile in this way
gcc -c -I/usr/include/lixml2 myfile.c
and everything is ok
g++ -c myprogram.c
and it's ok
g++ -o myprogram myfile.o myprogram.o
now I have some errors like this:
myfile.o: In function `parseDoc':
myfile.c:(.text+0xcf8): undefined reference to `xmlParseMemory'
(and other eror for other variable. I have this kind of error in each 4 function I have define)
myprogram.o: In function `main':
myprogram.c:(.text+0x257): undefined reference to `ValidateFunc(char const*, int, int)'
(the same for the other 3 functions)
I really don't know where problem is, and I'm really desperated because I can't go on with my work!
thanks
First of all I want to say thank you for help me.
now I have wrote the command you say to me and the result are:
for myfile.o (look myxml2 is the file that I have indicated with myfile)
RELOCATION RECORDS FOR [.text._ZNSaISt10_List_nodeIiEED1Ev]:
OFFSET TYPE VALUE
0000000d R_386_PC32 _ZN9__gnu_cxx13new_allocatorISt10_List_nodeIiEED2Ev
RELOCATION RECORDS FOR [.text._ZNSaISt10_List_nodeIiEED2Ev]:
OFFSET TYPE VALUE
0000000d R_386_PC32 _ZN9__gnu_cxx13new_allocatorISt10_List_nodeIiEED2Ev
.......................................
.......................................
.....................................
RELOCATION RECORDS FOR [.text._ZNSt4listIiSaIiEE5eraseESt14_List_iteratorIiES3_]:
OFFSET TYPE VALUE
0000001d R_386_PC32 _ZNSt4listIiSaIiEE5eraseESt14_List_iteratorIiE
00000038 R_386_PC32 _ZNKSt14_List_iteratorIiEneERKS0_
I forgot one thing: I'm using a c++ compiler , or better, when I link the two object file I use g++.
to compile c++ file I use g++ and to complile c file I use gcc. I specify this because at the beginning you tell me "If you compile with a C compiler,". I don't know if this information is useful.
struct indirizzo{
unsigned int ips;
unsigned int ipd;
unsigned int sPort;
unsigned int dPort;
unsigned int protocol;
unsigned int fid;
unsigned int ipsmasks;
unsigned int ipdmaskd;
};
extern int ValidateFunc(const char *buffer, int length, int socket);
extern struct indirizzo * parseDoc(char* buffer, int dim);
sorry! the previous solution is wrong! this is the right version:
the file .h must be
#ifdef _cplusplus
extern "C" {
#endif
struct indirizzo{
unsigned int ips;
unsigned int ipd;
unsigned int sPort;
unsigned int dPort;
unsigned int protocol;
unsigned int fid;
unsigned int ipsmasks;
unsigned int ipdmaskd;
};
extern int ValidateFunc(const char *buffer, int length, int socket);
extern struct indirizzo * parseDoc(char* buffer, int dim);
extern const char *XmlwriterMemory1(const char *file);
extern const char *XmlwriterMemory2(const char *file);
#ifdef _cplusplus
}
# endif
and in the C++ file we must write :
extern "c"{
#include "myxml1.h"
}
then
g++ -o out.exe -lxml2 myxml2.o mioprogramma.o