My Visual Studio Solution contains the following projects :
Solution
-> FolderName
C++ Project Name 1
C++ Project Name 2
-> C++ Project Name 3
-> C# Project Name
C++ Project Name 3 is a library type project (generates a dll and a lib)
C++ Project Name 1 and 2 use a header from C++ Project Name 3 and in the linker section they expect for the lib generated by C++ Project Name 3
If compiled one by one in the project order (3,1,2) everything is compiled correctly and works, but in case i try to compile the entire solution i get a linker error stating that it cannot compile C++ project Name 1 because it cannot find the lib from C++ Project Name 3 .
My question is how can i compile correctly (in the expected order) if i select compile solution ? Next step i have to do is to compile using the build system from TFS and i expect there i will have the same problem
In Visual Studio, go to the Project menu, then Project Dependencies. Select the project that "needs" the library, and check the library's check box. This will tell Visual Studio how to determine order or compilation.
Note: you can also manually change build order, but by setting dependencies, Visual Studio can work out the order itself, which may be more optimal.
MSDN: How to: Create and Remove Project Dependencies
It sounds like you need to set up your project dependencies for the solution. How to: Create and Remove Project Dependencies
Use the Project Dependencies, Common Properties, Solution Property Pages Dialog Box to set the current build order. To access this dialog box, select a solution in Solution Explorer, choose Property Pages on the View menu, and then select Project Dependencies under Common Properties.
https://msdn.microsoft.com/en-us/library/zk4ahe0t(v=vs.90).aspx
Related
My goal is to compile existing C++ classes (legacy code, stored in a set of *.h files) into a DLL so that it can be further integrated into a C# application.
For that purpose, it seems best to use MS Visual Studio. I have no experience with this environment, so I tried the naive approach found on MSDN and other SO answers:
File | New | Project from existing code
selected Visual C++
selected file location that is base for include references used in those .h files
specified a project name
let the wizard find and add all C++ files below the directory
selected "Use Visual Studio" for build, with project type "Dynamically Linked Library (DLL) project"
checked none of the checkboxes below (ATL, MFC, CLR)
specified . dir in the "Include search paths (/I)" in Debug settings
checked "Same as Debug configuration" in "Release settings"
clicked Finish button
This creates couple of VS files in the directory:
mylibrary.sln
mylibrary.vcxproj
mylibrary.vcxproj.filters
mylibrary.vcxproj.user
With a project created this way, I press F6 or select Build | Rebuild solution from the menu.
Then I expect the build to produce the .dll file somewhere, but it does not appear. Only these files appear:
.vs/mylibrary/v15/.suo
.vs/mylibrary/v15/Browse.VC.db
.vs/mylibrary/v15/Browse.VC.opendb
.vs/mylibrary/v15/ipch/AutoPCH/efad7c74cd39331b/EXAMPLE.ipch
Debug/mylibrary.log
Debug/mylibrary.tlog/mylibrary.lastbuildstate
Next, I decided to try creating a fresh new library project, just to observe the differences to get some hints, but that did not help - there were too many differences, even in the file structure...
My questions are:
is my choice of MS Visual C++ a good one for given purpose?
if so, what am I doing wrong here?
I think your steps are probably correct and I think that the right approach to use the code from a C# application. You definitely can call a C++ library from C# by importing the methods.
You missed only to export the methods that you want to use from your library. try using __declspec(dllexport) with these methods. please check this link:
https://msdn.microsoft.com/en-us/library/a90k134d.aspx.
Also, the output should be at the build folder, not the source code folder
Compiling .h files into libraries is ok, the compiler does not care - however, the UI does.
Still, you can tweak this by directly editing the .vcxproj file.
While doing so, make sure that the <ClCompile> sections contain:
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
Note that you can use commandline for building the DLL project:
"%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" -target:Clean,Build
(this assumes that your current directory is the one with your .vcxproj)
Background: In visual studio 2015, I have an project A built as an application. I want to create two packages B and C which wrap the contents of A into a DLL (in B) and into a runable application in C. After removing the main from A, I want to change project A from an application to a static library.
I tried changing the
Project Properties|Configuration Properties|General|Project Defaults
from Application (.exe) to Static Library (.lib). This leads to a linker error:
LINK : fatal error LNK1561: entry point must be defined
Obviously, Visual Studio still thinks that this subproject has to be compiled like an application (see here ). It therefore expects that there is a int main() which I don't want to have anymore.
So, how can I change the project type so that it is compiled as a static library?
There seems to be no way to change the build type from the GUI.
However, this can be done by altering the file A.vcxproj.
Search for all tags <ConfigurationType> and change them from Application (or whatever you have there) to StaticLibrary.
This only needs to be done for project A - the solution will adapt, the next time you start it in Visual Studio.
To properly link A into B and C, don't forget to:
1) Add a reference from B and C to A.
Open Solution>Project B/C>References, choose Add Reference, add reference to A.
2) Add a project dependency from B and C to A.
Open the context menu for any project and select Build Dependencies...>Project Dependencies>Dependencies. Add A as a dependency for both B and C.
In Visual Studio 2017, you can change the Configuration Type using the Project Properties.
With your project open, go Project / Properties.
Under Configuration Properties / General / Project Defaults, there is an entry called Configuration Type. The choices I see are Static Lib (.lib), Dynamic Lib (.dll), Application (.exe), Makefile and Utility.
My project was imported as a Dynamic Lib. I updated this setting and it now generates Static Lib. Related to the other answer, changes this setting in Visual Studio did result in the value within the project.vcxproj file to be updated. I figure doing it through Visual Studio instead of directly on the file would be more safe.
I have a very specific question in regards to visual C++ 2010 express. I have looked everywhere but can't find instructions on how to compile several source files. I have programmed on Unix at the command line and am trying to learn visual C++ 2010. I am using a header file that contains the function declarations and global variables. I don't know if this is the correct venue to ask this question but if anyone knows of some place where I can get the answer I would be grateful
Thanks,
Ral
If you have a project that you build from the command line with a makefile, then the Visual Studio development environment will not recognize your project. To open and build your project using Visual Studio, first create an empty project containing the appropriate build settings using the Makefile Project Wizard. You can then use this project to build your project from the Visual Studio development environment.
The project displays no files in Solution Explorer. The project specifies the build settings, which are reflected in the project's property page.
The output file that you specify in the project has no effect on the name that the build script generates; it declares only an intention.
Source: Creating a Makefile Project (VS2013)
On the menu: File->New->Project
On the dialog:
select Win32 Console Application,
enter Name ( like you did in the -o in unix) in the bottom,
and press OK
On the next dialog: Press next.
On the next dialog:
unmark Precompiled headers
mark Empty project
press Finish
Now find the Solution Explorer tree. You have Solution name and a project with the same name in it.
Right click on the project (not solution)
choose Add->Existing Item
and select your files, (you can copy them to the opened folder and then choose them)
press Add
Now you can try to compile.
Create a new solution with a C++ console command-line project
Create a new project, a C++ static library
Make the command-line project depend on the library
Make sure "Link Library Dependencies" is turned on in Configuration => Linker => General (it is by default)
Visual Studio will still not link the library.
How can I fix this? It worked in Visual Studio 2008.
This still works, but was changed in VS 2010:
"With VS2010, we stopped supporting project dependencies defining implicit references and we also introduced a new way of defining project dependencies at the project level. Since a project reference and a project dependency are close concepts, both applying to a project, it made sense to have them represented together, in a consistent way, in the project file. As you will see in the snippets below, the only difference between a project reference definition and a project dependency definition consists in metadata that defines the output assembly inclusion/exclusion into/from the main project link command line.
Although we did not remove the “Project Dependencies” dialog, we recommend defining new project dependencies via the “Framework and References” dialog. You need to set the “Reference Assembly Output” property in the property page UI to false for a project dependency and to true for a project reference."
Just right-click on the console project, select "Properties->Common Properties->Framework and References->Add New Reference" and add the static library project; also check that "Link Library Dependencies" is True on the right hand side. Seems to work for debug and release builds. You learn something new every day. ;)
They changed the UI for adding C++ project dependencies in VS2010, but oddly enough, without removing the old UI, or in any way indicating that it no longer works.
To create the dependency in VS2010, you need to use "Add New Reference" (can be found in project properties), and maybe also in the project's right-click menu (don't have VS here to check)
The old "Project Dependencies" dialog is basically broken now.
For MSVC 14 (2015 version) right-click on the project, then "Add->Reference..." and check all the needed dependencies.
Yes, it has changed somewhere between 2010 and 2015 versions. Fun!
And if you are looking to link a project that has resources in it - you need to specify the .res file directly in the list of linker input dependencies (project's properties dialog box) as it doesn't get picked up by the above configuration.
UPDATE
Still the same (new) behavior in MSVC 2017
I believe the old UI (dependencies) affects build order for Visual Studio, when building from within the IDE, for info. The new project configuration system embeds the references in each project file so that you can build from outside the IDE (whereas in previous versions, you could not, because you would not get automatic linking for dependencies, since dependencies were only done at the solution level).
There are also some issues with more complex projects in the new system; specifically, all resulting binary projects need to have explicit references to every dependent library to build correctly, whereas previously they could be effectively inherited from other dependent libraries. Same underlying cause, though.
I am using VB.NET 2008, and I want to make a DLL that will be used in a C# project.
When you build your class library in Visual Studio, it will generate the DLL for you and place it in the bin directory.
YourProject/bin/Debug/YourProject.dll
YourProject/bin/Release/YourProject.dll
It will be placed in the folder that represents your build-mode (Debug / Release), which you can normally switch in your toolbar.
Go to File > New Project and select Visual Basic, Class Library as the project type.
Enter your solution name and directory, and click on OK.
Voila!
Once you code your library and build it, you can go add it to your references in your C# project.
Just select a Class library as the project type.