Methods in linking C source code with C++ code
-
I have developed the GUI using Visual C++ and need to incorporate the functions into the application. The functions are written in C code. I have been trying to link the two codes together but things are not working fine. Approach I took: 1. Inserted the C source code project into my GUI application workspace. 2. Make the GUI application dependent on the C source code project. 3. Included the header files from the C source code project and the compilation was fine. 4. Transfer the functions in main program of the C source code to the GUI Dialog class. No problem in terms of compiling though I am not too sure whether this is correct. Use the following method for the declarations for the C functions in the header file. afx_msg void decode(void); afx_msg void Configure(void); For the function in the cpp file, I used the following method for the first function to be called. The remaining functions remain as C types function headers. If I remove the C++ function declaration, there will be errors as the rest of the C functions could not be recognized. void Dialogc::decode(void); void Configure(void); 5. No problem on compiling. However, during the linking up, there are some extern variables in the image processing files that need referencing to. They are declared in the Dialog cpp file. That is the reason for the "unresolved external symbol _img" and the strings of other error message. I have tried including the Dialogc.h in the source file of the image processing file and they could not recognize the class terms i.e. more error occurred. Thanks. lost in time
-
I have developed the GUI using Visual C++ and need to incorporate the functions into the application. The functions are written in C code. I have been trying to link the two codes together but things are not working fine. Approach I took: 1. Inserted the C source code project into my GUI application workspace. 2. Make the GUI application dependent on the C source code project. 3. Included the header files from the C source code project and the compilation was fine. 4. Transfer the functions in main program of the C source code to the GUI Dialog class. No problem in terms of compiling though I am not too sure whether this is correct. Use the following method for the declarations for the C functions in the header file. afx_msg void decode(void); afx_msg void Configure(void); For the function in the cpp file, I used the following method for the first function to be called. The remaining functions remain as C types function headers. If I remove the C++ function declaration, there will be errors as the rest of the C functions could not be recognized. void Dialogc::decode(void); void Configure(void); 5. No problem on compiling. However, during the linking up, there are some extern variables in the image processing files that need referencing to. They are declared in the Dialog cpp file. That is the reason for the "unresolved external symbol _img" and the strings of other error message. I have tried including the Dialogc.h in the source file of the image processing file and they could not recognize the class terms i.e. more error occurred. Thanks. lost in time
Adding C style code to C++ should be pretty straight forward. Though you should watch out for statements wrapped in #if defined, #endif blocks. Without seeing the code, it's hard to say, but I would guess that is where your error are coming from.
-
Adding C style code to C++ should be pretty straight forward. Though you should watch out for statements wrapped in #if defined, #endif blocks. Without seeing the code, it's hard to say, but I would guess that is where your error are coming from.
Thanks. I have included all the C style code files into the project and segregated the C code out from the C++ code. It has compiled successfully. As usual, after one obstacle is overcome, another one appears. Now I have a problem that is related to Winsock2.h. The below if-define statements were in the program. #ifdef WIN32 #include #else #include #endif It compiled successfully but during build, two errors occurred. rtp.obj : error LNK2001: unresolved external symbol __imp__ntohl@4 rtp.obj : error LNK2001: unresolved external symbol __imp__ntohs@4 The problem has to do with ntohl and ntohs that are defined in Winsock2.h. I have no idea what these two errors are. The program should not have a problem as a build was done in a separate project and it was successful. Please advise. Thanks. searcher08
-
Thanks. I have included all the C style code files into the project and segregated the C code out from the C++ code. It has compiled successfully. As usual, after one obstacle is overcome, another one appears. Now I have a problem that is related to Winsock2.h. The below if-define statements were in the program. #ifdef WIN32 #include #else #include #endif It compiled successfully but during build, two errors occurred. rtp.obj : error LNK2001: unresolved external symbol __imp__ntohl@4 rtp.obj : error LNK2001: unresolved external symbol __imp__ntohs@4 The problem has to do with ntohl and ntohs that are defined in Winsock2.h. I have no idea what these two errors are. The program should not have a problem as a build was done in a separate project and it was successful. Please advise. Thanks. searcher08