VS 2010 C++ creating library

51194e047aaef5716947139d3619ef37
0
qwasd 101 Jun 05, 2012 at 17:13

Hello mad devs, my first post in this place.
I would like to start to develop my game. Now im just for making engine. Im considering using SFML and C++ to do this, but this question is not about it.

I want to seperate game from engine.
So at first, i made sollition with C++ project into it. So far so good.

Then i added new project to this sollution. HERE my question begins. What settings do i need to change in engine project, so it compiles as library ? (.dll i guess ?)

And what settings do i need to change in game project (.exe) so i can access classes from engine library ?

School didnt taught me, how to do this. In C# its easy.

4 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jun 05, 2012 at 17:51

What IDE / project system are you using? Visual Studio 2010?

51194e047aaef5716947139d3619ef37
0
qwasd 101 Jun 05, 2012 at 17:54

yes

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jun 05, 2012 at 18:10

When you create a new Win32 project there should be an option in the project wizard to make it a library. To link it into your .exe, right-click on the .exe project, go to References and a reference to the library project.

If the library project is in a different directory you will also likely need to add its directory to the include paths for your .exe project (found in the project settings under the C++ group).

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jun 05, 2012 at 21:59

In VS C++, you see the tree view (on the left or right). You have one solution and one or more projects under it. Right click on the project and select “Properties” from the context menu. In the new window, expand “Configuration Properties” and select “General”. On the right side, you will see “Configuration Type”. You can chose Static (.lib), Dynamic (.lib + .dll), or exe. Depending on which one you chose, the window will update itself to show only properties valid for that project type.

If you want to link a library (static or dynamic), in the same window expand the “Linker” node and select the “Input” option. Make sure you add the .lib files inside “Additional Dependencies” section. Just type in the .lib file, not the path. To specify the path to the .lib files, select the “General” option under Linker and make sure you add the proper path to your libs in the “Additional Library Directories” property. If your DLL doesn’t have a lib file, you must do two things. 1) Contact the author and scream at him. 2) Learn how to load dynamic libraries at runtime.

To access the code in a library, there should be some sort of interface classes, or in the case with C there will be some functions tagged as “extern”. When you compile your project, there’s two stages. The first stage is the compile stage, which compiles your C/C++ code into .OBJ files. The second stage is the linker stage, which essentially combines your .OBJ files into an .EXE or a .DLL, depending on your project configuration. If a library declares a function and/or class/method and that piece of code was not compiled into the .lib file, you will get a linker exception stating certain things are undefined. You’ll have to do some detective work to solve those sort of issues.

If you want to keep things easy, build a static library. There’s no special requirements, just write your classes as you normally would. In your EXE, you can use those classes without requiring special syntax. For DLLs, you need to jiggle with the code a bit. You need to handle exporting and importing. Have a look at this site for some details.