VS Code, C++, cant link external library, undefined reference - c++

I have a problem and I can't fix it no matter what I do. Basically my university is using Stanford libraries for c++ and the only program that can use those libraries out of the bag is vs2008.
I'd prefer to use VSCode, but I can't seem to get it to work. There is a folder with .h files and a compiled .lib file. The header files have nothing in them except for function names. Thing is that vs code can find those header files and recognizes functions but can't compile any of it. I guess it can't link to .lib file. How can I fix this? I did everything I knew but nothing worked.

Related

How is linking failing: Undefined reference to library

I have inherited a code project that contains several individual libraries of code that compile separately and then are linked in the compiled tools. It's supposed to be a Chinese menu of what each tool wants. This all written on Linux, in C++, with Qt. There are several issues with the current design, but I'm learning to deal.
This latest issue has me really dumbfounded. The main library is the Utilities. It contains a handful of classes and it compiles into a .a library file. Another library is our DatabaseInterfaces. It has class files that refer to header files from Utilities (they are all shared) but the CPP files are not included in DatabaseInterfaces. DatabaseInterfaces also compiles into a .a library. Finally, we have a CMDPrompt tool that imports both the Utilities.a and DatabaseInterfaces.a libraries. CMDPrompt does not compile. Instead, I get several errors indicating that I have an undefined reference for one of the objects in Utilities.
After several different attempts to fix this, I finally directly included the CPP file in the CMDPrompt.pro. It worked or at least it is now finding new undefined references for other classes in Utilities. This confirms to me that somehow the projects are not linking correctly. I would have expected that because the Utilities library is linked in I would have gotten all of the H/CPP goodness with it. I suspect the problem is that the DatabaseInterfaces library is compiling against the H files only and needs the same Utilities.a library. I tried adding that LIB into the DatabaseInterfaces.pro, but it didn't have any effect.
I am not a C++ programmer by training and while I believe I understand the main points of the linking process, I am obviously missing something. Here are my questions. Given the relationship between the different libraries, how should the linker work? Why is the DatabaseInterfaces.a compiling at all with just the H files? What is the best way to resolve this issue?
You need to link the libraries in the right order - it sounds like Utilities.a needs to be linked last. I don't know how you do that in Qt but that's what you need to achieve.

Native to C++ CLI unresolved symboles

We have an excellent MFC/C++ project that handles the data to and from a legacy database. All classes are exported with AFX_EXT_CLASS (which is dllexport if i am not misunderstanding it). I am currently writing a wrapper so that we can use it in C# applications. This is my first real wrapper, so please bear with me.
This is what i have done:
Compiled the project with /Yc /Gm- /Clr. It produces a .lib and a .dll.
I have access to the source so, i added the path of the header files in my C++/Cli project. It therefore recognises the types.
I added the .lib to the linker input in the C++/Cli project.
I have added the .dll as a reference in the C++/Cli project.
In the C++/Cli project, i have a stdafx.h that looks like the stdafx.h in the native project.
If i understand this correctly, the .lib is the implementation, the .dll is the "wrapper" to be able to reference it as a .net-dll, and the header files are the class definition.
Now, i can define native objects in the C++/Cli project, but when i access functions therein and try to build it, it throws external reference not found errors on the objects that i am trying to define.
This is understandable because it can not know that the header files point to the implementation in the .lib. The header files are used as is.
What am i doing wrong? Am i supposed to point at the header files? Should it not use the .dll to find the class definitions that are implemented in the .lib file? Should i create a .def file to point at the library?
There seem to be a missing piece to this puzzle and i am expecting to experience an aha-moment any moment now...
Please note that this is not a general linkage question, it is specifically about the practical use of AFX_EXT_CLASS exported classes, MFC and C++/CLI.
Cheers and thank you
You don't need your step 4. Also, try to don't use Precompiled headers like stdafx.h in your project, Go to C/C++ -> Precompiled Headers -> Not using precompiled headers and Check Linker -> Additional Dependecies -> Inherit from parent or project defaults. Finally, check the error message link if it's point to windows library use missing try to include .lib library using #pragma comment (lib, "yourlib.lib")

Why do we reference C++ projects in Visual Studio 2013?

Why must we reference projects, for C++, in Visual Studio 2013 when we intend to use a method from a referenced project? I'm confused because we use the #include preprocessor directive, which is copying & pasting the code we intend to use into the same header file which is still above/before the method that is actually using it.
Basically, even if I have all the projects in the same solution file, and the header files are correctly finding each other (intellisense doesn't complain/ proper linting occurs), I still get linker errors. But even if referencing projects is purely about resolving linker errors, if intellisense can peek/show definition to the code I am importing from a separate project, doesn't that mean Visual Studio also knows which project I'm linking against?
There must be a reason why, we both #include and reference the included projects even now in VS 2013. Could someone help explain why?
"There must be a reason why, we both #include and reference the included projects even now in VS 2013. Could someone help explain why?"
Including header files allows to use the declarations (of functions, classes or structs) from there. But to get a completely executable program there's more necessary:
The compiler needs to know where to get the #include'd header files from
The compiled source code from a different project needs to be linked to the final artifact
The linker needs to know where to get the linked libraries from
If you have header only libraries, the least 2 points won't be relevant, but Visual Studio would still need the project reference for resolving the header file pathes.
For Visual Studio in particular there's also the #import directive available, that makes it easier to link against shared libraries (and keep those as standalone projects, instaead of referenced ones from the same solution).
No, it is computationally infeasible for a linker to magically guess which definitions you want to link into your project at any given time. It's a good thing that it does not attempt to do so.
The C/C++ compiling process has two steps:
compile (.c/.cpp) source code into a lib (requires external .h files)
link all necessary lib files into an executable (requires both the own compiled lib files and external lib files. After all, the external .h file only contains the definition)

Using a dll in a qt application program

Everybody, I am a beginner and I have still have some things I am confusing about.
I have a program qt which I want to include a extern library. Generaly, to include a extern library I use the macros :
INCLUDEPATH += "path/to/the/include/headers/file"
LIBS += -L"path/to/where/the/lib/are" \
-llibname // .a for gcc .lib for mscv
Then I can use the library in my program. But at the end the qt aplication program use the .dll associated to the lib name. So my question, why can't we use directly the .dll in Qt ? I don't know very much the difference between a ".lib" and ".dll" execpt a ".dll" is used at the runtime.
My problemen is have a library with only the dll and the include headers file. Is there a way to use this library like this or I must have the .a or .lib associated ?
EDIT :
Ok now,thanks to the useful advice, I understand better the difference between, .lib and .dll and how to use a dll without a .lib with only header. But I am having a issue. With the header, I can create the object, the compilator doesn't bother about it can't find the object and its method, but When I compile, I have some problems about "undefined reference to a method".
If I correctly understand, the reference cannot be found because the reference are defined in the .lib, that's why it can't find it. So my question :
How can I say to the compilator that the reference to a method will be defined at runtime and not at compile time ?
EDIT2 :
Ok so apparently you can get a pointer of an object with a dll but you can't use its methods, so I think I am facing the wrong way. Thanks again for you answer.
Best regards,
There are 2 types of lib file, see this asnwer: https://stackoverflow.com/a/2375144/2064646
You can just use a .dll without .lib file, but through GetProcAddress function.

GtkD (the Gtk+bindings for the D language) why compile it?

I've recently been using GtkD with the D programming language to create native applications. I've downloaded all the necessary files and got everything running so i can now compile and produce sample apps.
My question is that in some of the guides it tells you to compile GtkD on the platform you are using but what is the point? Once compiled you end up with a single lib file on Windows (GtkD.lib) and three lib files on Linux (ending in *.a). What are these files for and how are they used? Like i said everything seems to be working without doing anything with these files.
I'm guessing you can statically link these? but what's that for? To avoid compiling the GtkD source each time? I did actually try that using the pragma('lib', 'GtkD.lib') statement but it didn't seem to do anything.
Can anyone shine any light on this or explain why these files are needed?
Yes it's to avoid compiling the GtkD source each time.
Try to use pragma(lib, "GtkD"); rather than pragma('lib', 'GtkD.lib');
It works for me.
source files that are imported aren't compiled, they are only parsed for the symbol names and function signatures (and the templates)
creating libs is to avoid needing to recompile the entire library each time you need to rebuild and the compiler can then only use the .di files for the imports
this stems from the olden days where compiling took ages, and now it can be used to keep a library closed source