Include 3rd party library in Eclipse CDT with Visual Studio Toolchain - c++

I'm pretty new to using 3rd party libraries and such, and I need some help getting Eclipse CDT configured to link to the dynamic SDL2.dll library and be able to read the code from the header files in this 3rd party library. A lot of examples seem to be of linux system set-ups using GCC and such, but I'm trying to get it to work with the Visual Studio toolchain already installed on my PC.
Path to header files: C:\SDL2-2.0.5\include
Path to .dll and .lib files: C:\SDL2-2.0.5\lib\x64
Code:
#include "C:\SDL2-2.0.5\include\SDL.h"
int main() {
SDL_SetMainReady();
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)!=0){
SDL_Log("Unable to initialize SDL: %s",SDL_GetError());
return 1;
}
return 0;
}
I've tried adding the include path to the header files under Project>Properties>C/C++ General>Paths and Symbols>Includes>GNU C++(and GNU C), and I've tried adding the library path to "SDL2.dll" under Project>Properties>C/C++ General>Paths and Symbols>Libraries. The IDE still said it was unable to resolve the function calls; despite there being no issue with the #include "C:\SDL2-2.0.5\include\SDL.h" line.
I also tried adding the library path under Project>Properties>C/C++ Build>Settings>Tool Settings>Linker(link)>Libraries. Under "Additional libpath" i put: "C:\SDL2-2.0.5\lib\x64" and under "Libraries" I put: "C:\SDL2-2.0.5\lib\x64\SDL2.dll"; still with no success.
I even tried adding the include path to the .h files in the INCLUDE environment variable and the path to the .dll file in the PATH envirnment variable under Project>Properties>C/C++ Build>Environment.
Any help would be greatly appreciated.

On Windows, dynamic libraries have two parts: the .dll that contains the executable code and is needed at runtime and a .lib which is the import library that tells the linker to find the externals in the .dll. So, under "Libraries" you need to specify the import library (.lib). And put the .dll somewhere the executable can find it at runtime, e.g. in the same folder.

Related

C++, Visual Studio 2012, LIBs, DLLs, Includes, Source and Linker

I'm trying to understand what exactly all of these are and how they relate to each other (and most importantly, how to install them).
From what I've read, LIBs are libraries linked during the compilation of my project and DLLs are libraries linked during the runtime of my project.
So for me to use a LIB, I have to have the actual .LIB file somewhere in my computer, go to Project -> Properties -> VC++ Directories and add the path to the file in the Library Directories, and after this I have to go to Linker -> Input -> Additional Dependencies add the .lib name in there, and finally I need to type #include in my code, right?
So, some questions:
When I finish and build the release of my program, will the .exe only run if the target platform has the .lib installed in their PC as well? If yes, what steps do I need to do to make sure the .lib goes with the .exe?
When I get the source of a open source project, if I add them (using Add Existing Item...) to my project, can I use them just by using #include as if the files were mine and it would be the same as having the .lib installed? Or do I need to install the .lib file and still use these source files?
I have a project using OpenGL and I linked to glew32.lib, but I don't have the lib or any new directory added in the VC++ Directories, so I think this means I must've installed the .lib in the system folder or somewhere where the Visual Studio won't ask for another directory, should I worry about this when releasing a project?
How the above questions relate to DLLs and is there any reason why should I use DLLs over LIBs or the other way around?
I'm starting to use more and more libraries and I noticed I just dragged, copied and included it everywhere so I could use them but never really understood how they "fit" in the project. Especially those open source libraries where they provide so many files and I don't really know what to do with them...
You don't need to have LIB files along with your EXE file for running in another computer, LIB files are static files and DLL files are dynamic. So when you compile all static codes will be included in your EXE file, but DLL files will be loaded and used dynamically in runtime, so you just need to have your DLL files with your EXE file. This way, your code will work and run properly in other computers.
Just adding another project is not enough, you need to compile them and generate LIB files out of them. Then you add the generated LIB file to your final project and include external projects in your final binary. If you are compiling multiple projects together in a solution, you'll need to set project build order in solution properties in VS.
No, that's OK. It seems you've put LIB files in right folder and you don't need to have LIB file with your EXE file to run it in other computers.
DLLs are dynamic libraries, so you need to have them with your application. Installers usually install EXE files with DLL files in the same folder, so your app will run properly, but no need to include LIB files at all.
Also you can include LIB files like this:
#pragma comment(lib, "glew32.lib")
So you don't need to do it in project settings, but assuming you have your LIB file in "Library Directories" path.
Using DLL files can be done in two ways:
One is linking your application to DLL file and having DLL file's function entry in your EXE file's import table:
like using
#include <windows.h>
then
GetWindowsDirectory(windir, MAX_PATH);
So you'll have GetWindowsDirectory API entry in your EXE file's Import Table.
Also you can do it dynamically:
hinstDLL = LoadLibrary("kernel32.dll");
if (hinstDLL != NULL)
{
func_GetWindir = (DLLPROC) GetProcAddress(hinstDLL, "GetWindowsDirectoryA");
...
There is not much difference, only difference is:
In first method, as it's in your EXE file's Import Table, if there was no kernel32.dll or there was no GetWindowsDirectory entry in kernel32.dll, your EXE will not run at all, it will show a critical error and will not run. But in dynamic way (second way), your app will run, but as soon as your code try to use GetWindowsDirectoryA API, it will fail. You will have 0x00 in func_GetWindir. If you attempt to call it, then program will crash.

I can't make SDL + OpenGL work in Visual Studio 2012

I just started a tutorial to learn C++ OpenGL using SDL. The steps shown in the videotutorial were done by using the IDE called Code Blocks, but I thought it was going to work also for Visual Studio 2012.
I downloaded the SDL2 developer which is for Windows x64 (my Windows 7 is 64 bits, but the Visual Studio 2012 I have is an x86 version)
To install the SDL2 in Visual Studio 2012, I put:
the .h files in the include folder (D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\SDL).
the .lib files in the lib folder (D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\SDL).
the .dll file in C:/Windows/system folder.
After that, I opened the IDE, made a new project and while I was writting the code, autocompletion popped up for specific SDL features, meaning success... until I tried to run it.
This is the code I wrote according to the videotutorial:
#include "SDL.h"
int main(int argc, char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
When I click on "Start Debugging" or "Start without debugging", after pressing yes to build it, the output comes with errors.
Here's a screenshot of the code I wrote, plus the output:
After that, I tried an older version of the SDL the tutorial was already using, which was the 1.2 (I overwrote few files of the newer version) but still no good results.
How do I fix it so I can practice with OpenGL + SDL features?
TLDR:
When you use a library in your project,
Compiling needs header filenames (#include <*.h>) and include directories containing them
Linking needs lib filenames (*.lib, what you're missing) and library directories containing them
Running needs dynamic libraries (*.dll), commonly using the same name as the .lib in the current or system directory (VS has binary directories too but I've never needed to know what they're for exactly)
When you compile code and add #include <SDL.h>, the compiler runs through a list of possible directories its been given to find that file. There are some default directories already set up, but so you don't have a mess of all your libraries in one place it's possible to add more. For example, Setting Global C++ Include Paths in Visual Studio 2012 (and 2011, and 2010). If your SDL.h is in a subdirectory in an include directory it won't be found - eg. just check this shouldn't be <SDL/SDL.h> or something similar.
With the include path set and the correct #include your code can compile. Now it's the linking stage. You've got some code that says I need the these functions and the linker has to either find the actual code for those functions and link them statically (all the code can be found in the .lib) or set up the program to link at runtime, dynamically (in which case the program needs to be able to find the .dll, and I think on windows you need a .lib file that goes with it).
So in addition to the include directory you need a library directory. Also, just like you said to the compiler #include <SDL.h> you need to tell the linker to look for the code for those functions defined in SDL.h in SDL.lib (which may end up actually providing the code at runtime when loading SDL.dll). I think specifying libraries could even be done through #pragma calls, but it's more common to see the library files specified as arguments to the linker program. As #ScottMcP-MVP said, append SDL.lib to the Project Properties->Configuration Properties->Linker->Input->Additional Dependencies list. If it still says unresolved external that generally means the linker still can't find the file, so check for typos and double check the directories. Worst case is a mismatch between SDL.h and SDL.lib or SDL.lib was compiled with different options or a different compiler.
The error you're receiving is the linker saying the compiler says I need an "external" function, SDL_Init, to make your code work but I haven't found/resolved-the-reference-to one in the list of libraries I've been given.
Just as an example/recap all these GUI options end up going to the compiler/linker. So for example a full compile line might look like this:
./compiler -I/path/to/my/includes -L/path/to/my/lib -lmylibrary.lib mycode.cpp -o myexecutable.exe
Where mycode.cpp contains #include <mylibrary.h>. Note that here the ./compiler is operating as both compiler and linker. The process could be split into:
./compiler -I/path/to/my/includes mycode.cpp -o mycompiledcode.o
./linker -L/path/to/my/lib -lmylibrary.lib mycompiledcode.o -o myexecutable.exe
Once you have your executable it needs to be able to find SDL.dll when you run it. Exactly as you've done, the executable should search the windows system directory. It should also search its current directory. On linux you can set an rpath (not sure on windows). If you keep getting application configuration incorrect and the dll is definitely there it's time to grab dependency walker and see what dll name the program is actually trying to load. Worst case is the application finds a different version of the dll or something else with the same name before the desired one.
Good luck. OpenGL is great fun!!
[EDIT]
You can use either x32 or x64 lib files, but all your code must be the same. It'll probably be easier to start with x32, and if you need fast double computation and more ram then change. OpenGL doesn't have great support for x64 yet so it probably won't make much difference to you.
Additional Dependencies is where you put just "SDL.lib". This file then needs to exist in one of visual studio's library directories. If you don't get unresolved externals from SDL anymore then that part is fixed.
Your _main error is probably to do with a disgusting hack *spit* SDL uses. Straight after #include <SDL.h> put #undef main (may want to surround in #ifdefs in case it changes):
#include <SDL.h> //my include is <SDL2/SDL.h>, as I have both an SDL and SDL2 dir in my include path
#ifdef main
#undef main //remove SDL's main() hook if it exists
#endif
An important thing to note: generally use </> to include external libraries and "/" to include headers within your project - What is the difference between #include <filename> and #include "filename"?
You have to link the lib file into your exe. See Project, Properties, Link, Additional Dependencies. Put the name of the lib file there.

Netbeans C++ using MinGW and the libnoise library

Using netbeans 7.2 and the most recent version of MinGW (using installer) I can't use the libnoise library. I am properly including the header files, the auto completion confirms this, however the library is simply not working. There is a .lib file and a .dll. I have tried every possible combination of adding them under project > properties > Build > Linker as well as putting the .dll in the base project directory. Despite all this I am still getting undefined reference errors whenever I try and run the code. Does anyone know what to do?
I know that it is possible to link import library files (*.lib) with MinGW, but I still suggest to recompile libnoise.
With the current Makefile, this is not very easy and may break. Thus I've written a CMake script, which doesn't only work on Windows, but should work on all CMake supported platforms. In addition to this, I've cleaned up the directory structure. The code itself hasn't been touched and when you build a library it should essentially be the same as the current one.
libnoise on GitHub
After you've built your shared library, you'll have a libnoise.dll.a and libnoise.dll file. You then add libnoise.dll.a to the linking settings and put the DLL itself next to the binary, or in the working directory.
You have to link the lib file (= adding it to linker libraries) and put the dll to
<project root>/dist/<...>/
(where your exe is put to). There's no need to add the dll to linker too.
Can you please post the error message you get?

How to include SDL library in my VS 2010 project?

I encountered a problem with linking and distributing libraries in Visual Studio 2010. What I want to do:
Link my project with SDL and use its headers
Keep SDL somewhere in my project directory, so when I clone it from source control on another machine, I won't have to install any additional library.
My attempts so far:
Google points to tutorials like this one : Lazy Foo Productions, but it doesn't do what I want because SDL is placed outside project directory.
When I copy SDL to solution directory and add it via Project->Properties->Linker.... It works, but project settings now contain absolute path to library directory, so I guess it won't compile on other machine.
What should I do? SDL is just a example here, I'd like to know how to solve such problem with any library.
You can use Project->Properties->Linker->General->Additional Library Directories to specify new directory where the linker to look up for libraries (it can use relative path)
You can use Project->Properties->C++->General->Additional Include Directories to specify where to look for the headers.

Boost C++ Libraries linker error libboost_serialization-vc100-mt-gd-1_47.lib

I'm using Boost C++ Libraries v1.47 to serialize an class (I used the installer and selected everything and for VS2010).
But I get a error from my Visual Studio 2010 linker "Lnk1104 Can not open libboost_serialization-vc100-mt-gd-1_47.lib". I set up the "c:\program files\boost\boost_1_47" for my includes and "c:\program files\boost\boost_1_47\lib" for my libs. I don't know what's going wrong.
Update
I don't know why but if I put the lib files from the boost directory to "c:\program files\Microsoft Visual Studio 10.0\VC\lib" everything work fine. Boost load the lib files
somehow dynamical.
If you cannot find the file on your computer with the windows searcher just ask anybody else who uses boost to give you a copy of that file (libboost_serialization-vc100-mt-gd-1_47.lib).
My friend from work used to have the same trouble after having installed boost, there was just not the file in the boost folder. At the same time I picked up the file successfully so we copied it to the right directory on his computer.
For some reason precompiled libs in the installer are named on format "boost-module-vc100..." and headers specify library name as "libboost_module-vc100...". So you should specify library name in project settings to make it linked correctly.
I solved this issue by compiling all boost libraries by myself. With modern PC it won't take too long to do.
to resolve this problem,
go to properties -> Linker -> General -> Additional Library Directories
Enter the path of boost:
Boost-path\stage\lib