I'm trying to create a static library using Visual Studio 2005, consisting of multiple header and source files, for example:
A->B, A->C, A->D
B->E
(A->B = B is #include-d in A)
I managed to compile a static library by following the MSDN tutorial and put all the files in a project file in Visual Studio, and compiled a .lib. The problem is, I only want others to have access to one header file (A), and not the others, but because A includes the other files, after I have compiled the library and tried to use this in another project, when I only include the static library and header file from A, it says it can't find the header files for B, C, D and E. Is there a way to solve this issue?
One method (not the most elegant and simple method) I thought of is to put all the codes into one set of header/source files, but that's a lot of work and can get quite tricky...
Not without refactoring.
When creating libraries I like to arrange my header libraries into public and private headers. You can have a private header include a public header but not vice versa.
You're going to need to clean up your header files to cleanly separate the public and private parts of your API.
Unfortunately, without knowing why your header files are structured the way they are, I can't offer more specific advice.
When designing a library, it's important to separate the interface you want to expose from
the implementation of your library.
Just create a header file with the stuff you want to expose to users of your library, or e.g. just don't have A include the other header files.
Related
Visual Studio 2019. Windows Runtime Component C++/WinRT project.
How do I follow and implement the recommendation: "We recommend that you declare each runtime class in its own Interface Definition Language (IDL) (.idl) file, in order to optimize build performance when you edit an IDL file, and for logical correspondence of an IDL file to its generated source code files. Visual Studio merges all of the resulting .winmd files into a single file with the same name as the root namespace. That final .winmd file will be the one that the consumers of your component will reference."
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-apis
Within the same project I can add a second (third and so on) .IDL file using Project->Add New Item->Midl File (.idl). This seems to work ok?! I then manually add associated .h and .cpp files for each .IDL file. Build to generate stub files then manually copy and "fill in" these files before a final build which is successful. In this way I have a single IDL file per runtime class with its associated headers and implementation files including any other files (c++ .h and .cpp files) needed for the implementation. As I need to add functionality to the interface I just edit the IDL file, rebuild and add functionality to the header and implementation C++ files.
Is this what was recommended?!?
When I add the .IDL file and then manually add an associated .h and .cpp they do not appear "under" the .IDL file as the original ones did when I created the new project. Is this just a visual nicety of Solution Explorer OR is this indicating that I have something wrong?!?
I plan to have multiple "C++/WinRT WRC" projects (one for each namespace, each consisting of multiple runtimeclass interfaces with separate .IDL/.h/.cpp files as described above. Then I reference all of the projects (project to project) in my single C#/UWP App.
I am actually trying to implement a user interface to allow me to utilise a large code base of math/engineering C++ classes that implement a very specific & proprietary type of electronic circuit simulation already existing and programmed in C++ and compiled as a console app. The user data in and out is relatively small compared to the processing that takes place to generate the output.
It looks like you are handling the idl files correctly and that behavior is the same as I experienced when just adding a new Midl file directly.
If you like the look of having the .h and .cpp files "under" the .idl file in the tree you can try adding a new View Model instead of a new Midl file. This will create the .idl/.h/.cpp files all with the same name and place the .h and .cpp files visually under the Midl file. The idl template you get this way is for a basic runtime class.
When creating a new Windows Runtime Component, the .idl and the .h are usually grouped under the .cpp, not the .idl, but whatever your preference. There isn't a template to add a Runtime Class, which is pretty dumb. One fix for this could be as follows:
This is not a perfect solution, but is the easiest. Create a new Windows Runtime Component. Open Project>Export Template. Select Export Item. Select the Runtime Class under the new component. Export with Automatic Import option checked. Save your project and exit VS. Open your project. You can now add a fully templated Windows Runtime Class by simply adding item>Runtime Class. However, there will still be no dependencies (there may be a tool for custom nesting, but not in community as far as I can see). To fix this, after adding the files, save the project. Edit the project file (use notepad++ or even unload project, edit) and add the property <DependentUpon>Runtime Class.idl</DependentUpon> between your .cpp and .h file delimiters. Save the project file and reload. Probably want to do this in batches.
The alternative is to write a .vsix to either add the files and edit the project file, or have a right click command that runs on a .cpp and edits the project file, adding the DependentUpon directive. I can't find a 2022 functional .vsix for this. I will post one if I make one.
I'm using visual studio to create a game using the SDL library. I've also created my own static library that my game relies on. (It's the basic engine that sets up sdl, maps, ect.). Now when I include one of the files from my custom library, I have to type it like this:
#include <SDL_Game_Engine/Files/whatever.h>
Obviously that can be a bit tedious in typing all that.
So what I'm wondering is if there's a way in properties that will allow me to type
#include <Engine/whatever.h>
(Basically take the whole path to the file and make a shortcut for it).
I know it's a long shot but it would be awesome if there is a way. I don't like to have my include directory to include up to the files directory that way I know that the file I'm using is in my SDL_Game_Engine library. Is this even possible (with my luck there's no way lol) but any suggestions would be awesome. Thanks guys!
PS. Using Visual Studio 2015
Not sure what the SDL library is but..perhaps you can place things in an app.config and then assign a variables to your path;
String filePath = ConfigurationManager.AppSettings["YourFilePathKeyInAppConfig"];
then use it in code:
someGameVarFile = filePath; //psuedo code
Is there anyway within Visual Studio / TFS to identify which project produces which dll?
I'm aware you can look under a particular project's properties and see what the name of the dll is, but in the circumstance where you have loads and loads of projects this doesn't seem very efficient.
I've got the situation where I've got a project that references a dll, which includes a method I want to examine, but I don't know what project produces this dll.
Unfortunately, no. The only way I know is that you may could use a decompile extension. (Strongly not recommend to use) Through the source code after decompile, you can view namespace and judge which project produces the dll. (Under normal circumstances)
And you may also have to face some problems such as:
Legal issues
Need to pay for the extension
Only work for C#/.Net
The source code may be confusion and not standard
This should be a one time activity, you can go ahead and take a look into the project file, in case of C# project the csproj file.
If you do not want to do it opening each file, then i would say write a small tool to read all the project files and look for the name.
BTW, this will be different for different projects, and you need to find out the proper location to look.
When I created a new project Visual Studio 2012 created three files that I'm interested in: Stdafx.h, MyProj.h and MyProj.cpp. In MyProj.h Visual Studio created a "ref class" but I was never able to get the project to compile with the ref class there. I moved the ref class to the MyProj.cpp file, and the project now compiles fine. Why would a "ref class" be in the .h file instead of the .cpp file?
You have to understand the way C++ code is compiled and linked. It is very different from the languages you are used to, Java and C#. C++ doesn't have a standard way to break up your code into modules. Every source code file is compiled separately and produces an object file. Then they get glued together by the linker to make a program.
So if you have two source code files that use your class then they need to know what that class looks like. You do so by putting the declaration of the class in a .h file. Boilerplate is then to put the implementation of the class members in a .cpp file.
Which is what the project template gave you, a ref class in the .h file, ready for you to add member declarations to. And an empty .cpp file, ready for you to put the member definitions into.
This actually isn't that necessary anymore in C++/CLI because it is a language that actually does have modules. You know them from C#, called assemblies. And if other code, written in a different language, needs to know what the class looks like then you add a reference to the assembly. The compiler reads the class declaration from the metadata in the assembly, no .h file needed. But you'll still need a .h file if you have two .cpp files that get linked into a single assembly.
In general you'll want to pick up a introductory book about C++ programming. You'll need to know some of the basics to make effective use of C++/CLI. Such a book will certainly talk about header files and show how you split the declaration and implementation.
It's the same reason type definitions go into header files in normal C++: To help you keep definitions in agreement between all source files.
Header files are basically automated copy+paste, that guarantees you don't forget to change one of the copies.
(If different files ever start using different definitions for the same type, you violate the One Definition Rule, and the language gives no limitations on the bad things that can start to happen.)
I'm using Visual Studio to code a C++ program, with its source repository managed by SVN. I'm trying to have the solution compliable on multiple computers. The problem is, the libraries and include directories differ on both computers.
For instance, on one, the libraries are in (for instance) e:\devlib\boost_46_1\libs\regex\build, E:\devlib\SDL-1.2.14\lib etc and on the other computer in c:\programming\lib\boost_46_1\libs\regex\build and c:\programming\SDL-1.2.14\lib
Likewise for includes - which have the added layer of complication as to needing to retain the folders in the #includes in the actual source code (such as #include )
How do I structure my folders/source/vs solution/computer to make the solutions be able to compile on any of my computers without me having to separately list the library folders and maintain different project/solution folders for each?
I do understand one potentially valid solution to be include the libraries in the folder structure of the source, so that all the library folders can be, for instance "../libs" and "../inc" or something. However, given the sheer size of (for instance) boost, this solution is undesirable - especially with source control.
Place any third party libraries into a folder in your solution, place under source control and reference from this solution rooted folder.
Don't worry about the size; since these won't change that often, you will retrieve them infrequently.