adding reference to native visual c++ project - visual-studio

I have two visual c++ projects in my solution. First one (lets call it Main) is native code. The second one (Test), has Main added as reference. Test contains unit tests to methods in Main.
When I add Main as a reference to Test and try to compile it - I get errors that the library could not be found. Does adding a project as reference , does not add the output target path of Main to the library directories of Test ?

I don't know what VC is exactly doing under the hood, but adding reference to a project doesn't seem to have effect of linking libraries unlike C#.
You can use the code from another project by including and linking via the usual method of c++.

Related

Do you need dllexport macro for every single function in your library in visual studio?

I am new to Visual Studio, so if I say something wrong, please point me to the right direction.
I have a large C++ project that consists of shared library of around 20 classes and 7 executables, built by CMake in Linux. Each executable has its own CMake setup that links against the library.
I watched couple of videos on VS, managed to understand how to structure code and dependent libraries and successfully compiled the library statically.
I even managed to setup CMake in Visual studio, but started receiving an error that my .dll file is not Win32 application when I tried to build a project that links to it, so I dropped CMake and built the library by first making empty project and then adding necessary files (I thought it's CMake issue).
Long story short, now I have new setup for my library in VS that builds without issues. However I want my library to be dynamic because it makes ton of sense as 7 different projects depend on it and that's how I did it in Linux.
To my big surprise, even though selecting .dll will build, it cannot be linked to (I started all of this as empty project, not .dll template).
What I just discovered that building .dll library requires a macro that looks like (taken from https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170):
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
and before return type of any function in library I need to add the macro as:
MATHLIBRARY_API double some_function();
So if I understand correctly, this macro is "exporting" function names to .dll. Does that then mean that I need to open all 20 header files and wrap every single function in it with the macro?
Note that most header files are class declarations, do I need to wrap every single class method, just the public methods and is there a way to make a whole class "exported"?
It seems quite a cumbersome task just to build a shared library. I realize now this is why cmake failed and kinda begs the question on its cross platform capabilities if this wrapping to make .dll in VS needs to be done like this.
I am considering building my library statically, and in the same solution, add additional projects, and make a reference to the static library.
It kills modularity of the project, but it should work fine.

Xamarin binding library combination of InputJar and ReferenceJar?

I am trying to use https://github.com/Redth/Xamarin.Android.Xposed in Xamarin but the final result is rejected by Xposed, saying: "cannot load module" .. "the xposed api classes are compiled into the module's apk". The xposed-api.jar file is currently a EmbeddedJar, which is what causes that error. The xposed-api.jar file is made available during run-time by Xposed already.
From the xamarin docs.
InputJar – Does not embed the .jar into the resulting Bindings Library .DLL. Your Bindings Library .DLL will have a dependency on this .jar at runtime.
InputJar seems like it will solve my issue with it being compiled into the resulting apk. However, it can't compile into an apk at all. It gives me errors in the generated java file such as.
JAVAC0000: error: package de.robv.android.xposed does not exist
de.robv.android.xposed.IXposedHookLoadPackage 0
Then for ReferenceJar..
ReferenceJar – Specifies a reference .jar: a reference .jar is a .jar that one of your bound .jar or .AAR files depends on. This reference .jar is used only to satisfy compile-time dependencies. When you use this build action, C# bindings are not created for the reference .jar and it is not embedded in the resulting Bindings Library .DLL.
Great, it will provide it during compile-time.. however, no bindings will be generated so I can't use it.
I also tried combining these by creating 2 identical libraries with one having InputJar and the other ReferenceJar, but that did not work at all.
How can I achieve what I am trying to do?
To sum it up, I need to add xposed-api.jar as a library just like how android.jar of the Android framework is added. You can use the classes and compile properly, without the classes being compiled into the apk.
OK, The trick is actually "AndroidExternalJavaLibrary".
Imagine you have a Jar binding project XPosedAPI, and an Android project XposedSample.
The structure would be:
XPosedAPI
|-- Jar
|-- api-82.jar (InputJar)
XposedSample
|-- Jar
|-- api-82.jar (AndroidExternalJavaLibrary)
You see, the trick is you have to put the same jar file for each project.
For the Jar binding project, it has to be "InputJar" - that's used to generate the C# binding dll.
For the project that using the Jar bindng dll, the jar has to be "AndroidExternalJavaLibrary". This ensures the javac compile process.
There was an easier way, the Java.Interop.DoNotPackageAttribute.
You just put the below line on one of these projects (just put it on XPosedAPI project would be enough), and it works:
[assembly: Java.Interop.DoNotPackage("api-82.jar")]
However, this attribute is marked as "deprecated", although it's easier.
I figured out this, because I'm trying to do the same thing - write a xposed module in C#.
The Xamarin.Android.Xposed project looks nice, but in fact it won't work, and there are much more problems to be fixed besides the jar binding issue.
I think I'm getting closer, and I may publish it in a few days if I can fix all the bugs remained.
If you're interested, watch my project: https://github.com/UlyssesWu/XamarinPosed

Visual Studio : create a DLL that uses another DLL

I am building a DLL using visual studio, which involves installing the following libraries :
GLM
GLFW
GLEW
I linked those libraries to visual studio using the following method :
specifying Additional Include Directories in the project property page
specifying Additional Dependencies in the project property page
specifying Additional Library Directories in the project property page
Of course GLM is a header only Library, which means that I am only required to specify the Additional Include Directories for GLM. And my dll built perfectly fine.
But the real problem occurs when using the library in a test project. I linked my test project to my library using the method mentioned above, but when I tried to build the test project, it produces the following results :
Cannot open include file <GLFW/glfw3.h>
And the same goes for glew. It seems that these libraries are not found when the library is being used by another test project. How can I fix this? Any help would be highly appreciated.
Set the Additional Include Directories correctly for all projects. The compiler doesn't magically inherit settings from a project which happens to have it's output linked into another project. So you have to provide it the correct include path for any source file it sees. To spare yourself from having hardcoded paths to include directories you could use a property sheet common for both projects. Or you could tackle the problem in code and make use of the PIMPL idiom (eventually as simple as e.g. forward declaring some GL types and using a unique_ptr to them in public classes) so the headers of your project never expose any of the external include files.

Regarding linking a c++/cli dll and a c++/cli exe project

I have a native c++ dll called native.dll.
I have created a c++/cli project called cliWrapper.dll. In this I have two wrapper classes for some classes in native.dll.
And the compilation for this project works fine.
However, when I try to link cliWrapper.dll with my c++/cli console program, the linker complains that I must compile cliWrapper.dll with the compiler option /clr:safe.
Compiling with such option will generate lots of errors since most of native.dll is not verifiable code.
After googling, I see that linking two .module files requires each is compiled with /clr:safe.
Does that mean it is impossible to make a dll that will allow the user to use the wrapper class to do some stuff? I know I can always put those wrapper classes back into the console project and it will compile without problem but I'm just curious about why Microsoft wants to disable such linking?

Makefiles in Microsoft Visual Studio 2010

I am getting some linking errors during the compilation of C project in Microsoft Visual Studio 2010.I am getting the following errors:
error LNK2019: unresolved external symbol _CreateRelation referenced in function _main
The CreateRelation is one of the functions in my project. Following are my questions:
I think it is some dependencies problem.How would I set those dependencies rule in the IDE?
could you please tell me, is it always possible to build a project and set the linking rule, how much it is larger, without using makefile?
[EDIT]
relation.h
void createRelation(LIST);
mainfile.c
#include relation.h
#include xyz.h
.
.
.
int main(){
LIST Relation1;
some codes //
createRelation(Relation1);
some code //
}
The function creatRelation() is defined in the realation.h.
EDIT 2
In the function containing main
There are a few ways to set the dependencies for the build process.
If the code you are referencing is in a sub-project you can simply tell VS the build-dependencies. You do that by right-clicking on the project and select project dependencies. Then you can check all projects that should be built before this project is being built.
Another nifty feature of VS2010 are Property Sheets. In older versions of VS you had to tell the compiler the include path and the lib-path for every project. Now you can create property sheets for every library you are using and then simply adding them to your project. This way you only have to create a property sheet once and can use it in many projects.
So if the code is in another project that is not a sub-project you have to set the lib-path and include-path via those property sheets. You can display the property sheets used by your project by clicking View->Additional Windows->Property Manager
If you are not referencing to any external projects. This problem is most likely caused by you not implementing a function you declared. So the compiler knows about the function-prototype and doesn't complain but the linker can't find an implementation of the symbol.
I hope that helps
-- edit --
Since you said that the implementation is in the same file as the main-function I would suspect that the signature of the declared and defined function do not match. Are you getting any warnings about implicit function declaration?
Is that a copy-paste error?
CreateRealtion(x); vs. CreateRelation(x);

Resources