0

I have a open source project written in C and learning to wrap it with C++. All the functions used in C are defined in header and I have linked those in my C++ code as static library (having include and lib files). Now if i try to access/use function for example display() written in C open source from my main.cpp it is throwing undefined reference error.

Note: C open source and wrapper directories are not in same location. Just copied include and lib and linked to C++ project.

Error texts

CMakeFiles\wrapper.dir/objects.a(main.cpp.obj): In function `main':
G:/Development/wrapper/main.cpp:34: undefined reference to `IedConnection_create'
G:/Development/wrapper/main.cpp:36: undefined reference to `IedConnection_connect'

IedConnection_create is a function written in open source and have included the corresponding header in my C++ code.

Can anyone help me out of this ? Thanks

Vimalan E
  • 351
  • 3
  • 12
  • Does [this article](https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c) help? – Denis Sheremet Jan 29 '20 at 10:41
  • 2
    Do you remember to declare the C functions as `extern "C"` when building your C++ code? – Some programmer dude Jan 29 '20 at 10:41
  • Yeah i declared like below ``` extern "C"{ #include "filename.h" } ``` only one header i have included for now and which has the function prototype. – Vimalan E Jan 29 '20 at 10:44
  • 2
    Please try to create a [mcve] to show us, containing both the C and C++ source needed to replicate this problem (but not more). Also please include the `CMakeLists.txt` files used to build both the library and the application and (more importantly) *link* the library to the application. And please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 29 '20 at 10:47
  • My wrapper code can be found here https://repl.it/repls/NavajowhiteLumberingComments And open source C code https://libiec61850.com/libiec61850/wp-content/uploads/2019/10/libiec61850-1.4.0.tar.gz – Vimalan E Jan 29 '20 at 11:05
  • Did you link with both the wrapper and the original library? – user253751 Jan 29 '20 at 11:30
  • Probably unrelated but shouldn't the file `CMakeList.txt` be named `CMakeLists.txt`? – Ted Lyngmo Jan 29 '20 at 11:33
  • huh ! in my system it is correct. what you see in that provided link is just for reference – Vimalan E Jan 29 '20 at 11:42
  • @user253751 sorry ! am not getting what you are trying to say. – Vimalan E Jan 29 '20 at 11:45
  • Which command do you use to link the program? – user253751 Jan 29 '20 at 11:51
  • you mean linking lib and include ? – Vimalan E Jan 29 '20 at 12:00

1 Answers1

0

The linker cannot find the referenced symbols. Have a read here: https://latedev.wordpress.com/2014/04/22/common-c-error-messages-2-unresolved-reference/

KautschuK
  • 9
  • 1
  • I have gone through that article but it shows only commands i use clion ide. Any help on that ? Bringing to your desk I have wrote cmake to include lib (.a) file as well. – Vimalan E Jan 29 '20 at 10:47
  • do you also have something along the lines of this: **link_directories()** added to your CMakeList.txt? – KautschuK Jan 29 '20 at 10:58
  • 1
    link only answers are not nice. Can you summarize the article you are refering to? – 463035818_is_not_an_ai Jan 29 '20 at 11:18
  • My wrapper code can be found here repl.it/repls/NavajowhiteLumberingComments And open source C code https://libiec61850.com/libiec61850/wp-content/uploads/2019/10/libiec61850-1.4.0.tar.gz – Vimalan E Jan 29 '20 at 11:22