I am working on a game engine project in C++ with VS2010. We have one main project, OgreProject, which includes some Ogre3D stuff for rendering. Then, we have a class library project called AudioLibrary. AudioLibrary uses fmod, and has includes to the appropriate headers and libs. The problem arises when a class in OgreProject wants to use the SoundPlayer.h in AudioLibrary. Then, OgreProject does not know where #include is. It feels wrong to tell OgreProject where fmod is, since it will not directly use these headers. What is the correct way to using header files from AudioLibrary in OgreProject, without OgreProject knowing of ?
There is no correct way. There's no magical way for one library to know about the other library; you'd have to configure them to do that. If you put them in the same solution you can add one project to another as a project reference.
You might try the Pimpl idiom (or pattern).
It will let you remove everything related to fmod from your project's header file. Only the implementation files will need the fmod headers, not client projects.
See this answer which explains the benefits.
You should probably define a heirarchy for all the components of your project and keep all the header files from a particular component which other components are going to use at a pre-defined place. Other components can then always look at this place. There is nothing wrong in telling the components where to look for these dependencies explicitly
Related
I have a main project that has several 3rd party libraries. I just added a new 3rd party project. The problem I'm facing is the new project breaks when the main project compiler definitions are passed to it.
How can I avoid passing these definitions to the 3rd party project?
Also, if the 3rd party project is compiled stand-alone, it only requires two include directories. But when compiled with the main project there are many extra directories passed to it.
I've tried looking for a set_definition() but I'm not seeing anything helpful.
Any links to examples or write ups will be nice. It might also be that I'm not looking for the right solution.
thanks
Using global 'add_definition' is old-approach. In present may be much better define compiler flags, defines etc. per target, folder, source files etc. (in your case COMPILE_DEFINITIONS?)
Just google for 'modern CMake' and you will find slides and videos from conferences how to use modern CMake.
I have an object file library that exists as a standalone VC++ solution. I have a number of other completely separate VC++ solutions, and I would like some of them to utilise the classes included in this library.
However, since they are in the same solution, I cannot seem to add them as a dependency. I have attempted to investigate so-called "linker" dependencies but can't get it to work.
Does anyone know of a standard, modern efficient way to do this. Eventually my plan is to conglomerate these projects into a single solution as I believe they should be, but that is not something I have the time to deal with right now.
I think you are mixing terms project and solution. Generally speaking, solution is a collection of projects, with specified dependencies between them.
Library should be a project (possibly dependent on another projects), but not a solution. If you would like to include your library in another solutions, simplest way to do that would be to add project for library (and any dependent projects) to solutions you would like to add them by right clicking on solution->add->add existing project->add your project. This will ensure library will be compiled as part of solution.
You would need to set dependencies between projects (in your solution), and add include/linker path for the library to any projects using it within the solution.
I have some C++ code (exposing a C-only interface through a header) which I will use for an iOS project.
I can either create a static library and link to this from my app project, or add the source files directly to the app project - which option is best?
All answers appreciated!
Add the sources if you expect them to change often. Otherwise a library will be more suitable and will make your project cleaner (however, you will have to put only the header files in your project)
I've used OpenCV in one of my app projects which is mostly written in C++. I've found that adding the source files to the app project worked better for me because I made some minor changes to the code wherever appropriate. Comes down to the use case basically.
I always prefer to add the source if I have it, simply because it makes debugging easier. If you're making a call into a library routine and getting back an unexpected result (or crashing, or whatever), it's much easier to step into the library code with the debugger and figure out what's going on. If you just have a static library, it's a black box and you can't see what's going on inside. It also allows you to change the library code more easily if you encounter a bug or a missing feature (just be careful if the library is shared among other projects, to make sure you keep the library code up to date in its own repository).
Xcode is good about letting you keep your project organized, so use those features to your advantage. Keep the library code and headers separate from your main application and link it in as needed.
I suppose by code you don't mean a well formed library, so I expect this code could get any kind of modification pressure in the future. The best way is then wrapping it. here is one very nice example, but you might do it differently: http://robnapier.net/blog/wrapping-cppfinal-edition-759
Currently my visual studio is basically generating Engine.dll and Game.exe
Engine.dll links to some other basic libraries like:
d3dx9d.lib
ComCtl32.lib
WinMM.lib
WSock32.lib
etc.
I also wanted to try to create an Engine.lib, but I get some really nice warnings now: Symbol x has been defined already. These libraries define the same symbols.
So I read somewhere that I must force my user (Game.exe) to link to the libs instead. But this is just really inconvenient I think, especially if I have many games and I decide to add another library to my engine. It's just maintenance for something so simple.
Should I stick to the .dll, or is there some way to fix this beauty up?
Thanks a lot,
Antoon
You need to make up your mind whether the want the DLL or the static link library. Advantage of a DLL is that the build times can be quicker if you make local changes. Advantage of a .lib is that you'll end up with only one deployable file.
Getting it linked (either the static .lib or the dll's import .lib) is otherwise automatic. You want to make sure that the library is built first, can't link the .exe without it. Right-click the exe project in the Solution Explorer window, Project Dependencies, tick the library project. That automatically adds the .lib to the exe project's additional dependencies.
Using #pragma comment(lib, "engine.lib") in the engine's header file is another way. Repeat for other dependencies like the OS import libraries. Getting the library path right is a // todo item.
Did you create a different namespace to avoid naming clashes?
EDIT -- seems like there is some confusion as to what you're asking, because you are asking a couple of questions.
To me, it sounds like you want to take a stab at implementing your own Engine class. However, you've got naming issues. I am treating this as more of an architectural question now. If you write your Game to an interface, then the problem goes away. For example, if your Engine is using different algorithms, then if you had written an EngineInterface that the current Engine and YourEngine implemented, then you could easily use Strategy to flip between the different implementations on the fly. This is nice because you'll be able to see the differences in-game if you wire the preferences into the app.
If the symbols are not supposed to be the same, use diferent names or control how they are exposed. Another option is the usage of namespaces to avoid naming conflicts.
If the symbols are supposed to be the same thing, you need to define those only once in one of the libs.
I am developing a C++ library that I want to pass on to my team. The library has just one class with a bunch of methods.
So, I have developed the class definition file (X.cpp) and a corresponding class declaration file (X.h).
Here are my questions --
In Visual Studio 2005, whats is the most straight forward way to build this library as a DLL, such that I get the following files:
X.lib : which I can pass to my team so they can link against my library
X.dll : which I can pass to my team for run-time
Instead of a DLL, should I rather be going the static library way ?? If so, how do I go about doing this in Visual Studio 2005 & will this give me a X.lib file that I can pass on to my team ?
Any explanations or references are very welcome.
Thank you very much.
The easiest way to build a DLL, is New->Project->Win32 Console Application. Then on the dialog select DLL and select "Exports Symbols". This will synthesize a dll with a .h, and .cpp file which demonstrate how to export your classes. You can get rid of this .h/.cpp but first import your class add the appropriate #ifndef statements. Now as far as DLL versus Static library if its a single small class, that doesn't change particularly often you might be better off with a static library, its simple, its concise, it doesn't add another dependency which needs to be shipped with your product. A DLL is nice if the code in the .cpp file changes often (ie. the function implementations) because you can just swap in the new DLL.
From your description, it looks like you already have a Visual C++ project (correct me if I'm wrong). If so, when you go into project properties, under "General" you can find "Configuration Type" - switch it to "Static library" or "Dynamic library" as needed.
If you choose "Static library", then you will just get a .lib file that can be immediately used.
If you choose "Dynamic library", and you export any functions from your DLL (e.g. by marking them with __declspec(dllexport)), an export .lib will be generated automatically.
It's hard to tell which option is preferable without knowing the specifics of what you're doing. In general, I'd recommend defaulting to static libraries, because it's usually good enough, and there are more traps when dealing with DLLs (especially ones that export C++ symbols).
I think that, in most cases, a dll is a better choice than a static lib because your team will not have to recompile their code when you distribute a new version of your library.
I struggle with this too sometimes.. because I can't find where the elusive setting is within C++ Project Properties.. So I decided to jot it down for my own sanity as a blog post.
HTH