0

So I'm using Code::Blocks right now as my IDE on Windows 10 and I made a small library with one little function.

Code::Blocks made me a .a file.

I then made a little test project that uses the library. Added it to my project build options in Linker Settings, and added its directory to my search directories. Great.

I can see the header file I made for the library so that's working.

I then try to use the function and I'm getting an 'undefined reference' for that function call.

So..I'm going to assume Windows doesn't understand how to load libraries with the '.a' extension? I believe Windows uses .lib files instead but I'm not sure how else I'm supposed to compile a .lib file in Code::Blocks as there's no template for it.

I am extremely new to Code::Blocks and making libraries in general.

Aran Bins
  • 449
  • 10
  • 20
  • What's the toolchain used? Also read here please: http://stackoverflow.com/questions/24715864/problems-importing-libraries-to-my-c-project-how-to-fix-this – πάντα ῥεῖ Aug 18 '16 at 09:17
  • I'm using GNU GCC I believe (I think thats it) – Aran Bins Aug 18 '16 at 09:40
  • Can windows even use .a files? Why is there no option to make a .lib project in Code::Blocks? I also looked at the topic and yeah I did everything he explained to do to get a library linked already in my project settings. – Aran Bins Aug 18 '16 at 09:44
  • Neither *.a* nor *.lib* are file formats. Those are file extensions, with little to no relevance. You need to understand, what file types you need/want. As for *"I'm not sure how [...] to compile a .lib file in Code::Blocks as there's no template for it"*, please read [Does Visual Studio Rot the Mind?](http://charlespetzold.com/etc/DoesVisualStudioRotTheMind.html). – IInspectable Aug 18 '16 at 10:04
  • Ah well unfortunately I can't seem to get past this undefined reference. I've compiled the .a file no problem, as soon as I link it and try to use the function within, it just throws an undefined reference. I've definitely added it in the build options, not sure what could be wrong. – Aran Bins Aug 18 '16 at 10:13
  • Okay well I got it to work by putting extern "C" { .. } over the function declaration. But why does the library code have to be written in C? I tried changing the file name of the library source from .c to .cpp and removing the extern "C" { ... } code in my main project but now it's back to having an 'undefined reference' error? – Aran Bins Aug 18 '16 at 11:33
  • `extern "C"` is a [language linkage](http://en.cppreference.com/w/cpp/language/language_linkage) specification. It doesn't pertain to the implementation. This is really, really basic stuff. You need to get a [book](http://stackoverflow.com/q/388242/1889329) or two. – IInspectable Aug 18 '16 at 14:57
  • What does this have to do with the undefined reference though? I've changed the extension of the file to .cpp and removed extern "C" and now I'm getting an undefined reference as soon as this happened. – Aran Bins Aug 18 '16 at 16:49
  • Because the language linkage controls, how a symbol is decorated. `extern "C"` and `extern "C++"` cause symbols to get different decorations. The linker matches decorated symbols. Incidentally, it's also the linker that produces the *undefined reference* error. – IInspectable Aug 18 '16 at 21:19
  • Okay here's where I think the confusion was set in. I assumed the library I made was compiled with g++, but it wasn't. It was compiled with gcc. In Code::Blocks when I renamed main.c to main.cpp, it didn't switch the compiler to use. This was solved by going into the file properties and switching the compiler variable "CC" to "CPP" and now it compiles using g++ and it works without having to use extern "C". I was just confused why I had to use extern "C", thinking the library was a C++ library and not a C library. So it's all good now. – Aran Bins Aug 18 '16 at 23:44

1 Answers1

0

Solved. Problem was when renaming the file from "main.c" to "main.cpp", Code::Blocks does not automatically change the compiler variable.

This was done by going into the file properties in the IDE and changing the variable from "CC" to "CPP".

Aran Bins
  • 449
  • 10
  • 20