0

I followed the ways mentioned in Link object file to my project Eclipse CDR

My main function is in cpp code and from that I wish to call a c-function. So for that I created the object file (.o) from the c-code using

gcc -c -fpic mycode.c

This gave me a ".o" file which I wanted to link to the cpp code. I created a header file with reference to http://www.parashift.com/c++-faq/include-c-hdrs-nonsystem.html

Header file was declared as an extern in my cpp code. The header file contains to function from the c-code, that function was declared as an extern in header file.

After doing this when I do make for my entire project I am getting error as "undefined reference" to that c-function name

Community
  • 1
  • 1
user3555809
  • 31
  • 1
  • 1
  • 5
  • Do the declarations in the header file have the appropriate `extern "C"` or `extern "C" {...}`? Since the code was C, the name mangling will be C, but the C++ compiler will be looking for C++ name mangling. – Niall Sep 16 '14 at 10:48
  • This may help with the mix of C and C++ linkage; http://stackoverflow.com/a/12574420/3747990 – Niall Sep 16 '14 at 10:50
  • Gave as extern "C" { #include "foo.h" } in Cpp code and in separate header file as "extern "C" int foo_main();" – user3555809 Sep 16 '14 at 12:36
  • got the issue resolved my header file looks like #ifdef __cplusplus extern "C" { #endif Also the header i included in my main cpp file as extern "C"{ #include "mycode.h" }... thanku #ifndef mycode_h__ #define mycode_h__ extern int mycode_main(void); #endif //mycode_h__ #ifdef __cplusplus } #endif – user3555809 Sep 20 '14 at 04:00
  • Also see [Link object file to my project Eclipse CDR](https://stackoverflow.com/q/11810701/608639), [Is it possible to import/run object files in eclipse?](https://stackoverflow.com/q/30086902/608639), [How to link object (.o) file in Eclipse](https://stackoverflow.com/q/23396199/608639), [Include object file or assembler file in C Project?](https://stackoverflow.com/q/45338665/608639), [Adding object file to cpp code in eclipse](https://stackoverflow.com/q/25866628/608639) – jww Mar 23 '18 at 20:46

0 Answers0