Problem using a COM interface as parameter - atl

I have the following problem:
I have to projects Project1 and Project2. In Project1 I have an interface IMyInterface. In Project2 I have an interface IMyInterface2 with a method that receives a pointer to IMyInterface1.
When I use import "Project1.idl"; in my Project2.idl, a #include "Project1.h" appears in Project2___i.h. But this file does not even exist!.
What is the proper way to import an interface defined into other library into a idl file?
I tried to replace the #include "Project1.h" by #include "Project1_i.h" or #include "Project1_i.c", but it gave me a lot of errors.
I also tried to use importlib("Project1.tlb") and define my interface IMyInterface2 within the library definition. But when I compile Project2PS project, an error is raised (something like dlldata.c is not generated if no interface is defined).
I tried to create a dummy Project1.h. But when Project2___i.h is compiled, compiler cannot find MyInterface1. And if I include Project1___i.h I get a lot of errors again!
Apparently, it is a simple issue, but I don't know how to solve it. I'm stuck with that!.
By the way, I'm using VS2008 SP1.
Thanks in advance.

Don't include the *_i.c file from a header. You can do so from a source file (.cpp) as a simple way to get it linked in.
EDIT
You can treat separate IDL files and their products completely independently. There is no need to worry about combining them any earlier than at the stage where you include the ordinary header file generated from each IDL file.
To deal with IDL, you add it to a project. This causes MIDL to run on it, and output a header file. (It will by default also output other things, but they aren't essential).
I recommend that you make your IDL files write their headers out to a single, common include directory, which you can then add to the include path for any projects that need to work with the interfaces.
Then just use #include to pull in the header file for each interface you need. As the common include directory is on the project's path, the nested #include in header2.h will be able to pull in header1.h.

Related

Custom precompiled headers causing compile errors

I am trying to introduce precompiled headers into my project because of long compile times in my project right now. I have a main project that includes a main function and all the real code is in the DLL project(they are linked together.) I named my precompiled header "vpch.h" and i created a vpch.cpp that includes vpch.h. I am using visual studio 2017 so I went into the properties of vpch.cpp and selected Create. Then I added vpch.h as the first thing in all my cpp files. All files are set to use precompiled headers and reference vpch.h Every CPP files throws the error:
Error C1010 unexpected end of file while looking for precompiled header.
Did you forget to add '#include "vpch.h"' to your source?
I am at a loss as to what to do because I have followed many tutorials and can't find a reason for this. As well as most issues that pop up on google are people just accidentally including precompiled headers.
My only thought is that maybe in the section in properties where it asks for the name of the precompiled header, I need to do more than put "vpch.h" and maybe an exact file location? Any help with this is super appreciated.
EDIT
From further debugging it would appear that all but ONE cpp file is throwing an error. The one that doesn't throw an error is the one that exists in the same exact folder as the vpch.h. So files that can't just write #include "vpch.h" and have to write something like "../vpch.h" I can write #include <vpch.h> and I am going to try that now but I am unsure that will help.
The issue was with every CPP file that wasnt in the same folder as the precompiled header.
So if you use a file structure that contains different classes in different folders, using
#include "../../vpch.h" will actually fail. You must add the root folder to your additional include directories and then use #include <vpch.h> for all files. I can NOT tell you why using #include "../../vpch.h" wasn't working. It seems counter intuitive for it to fail in my opinion.
It may be because it searches for the precompiled header in the same folder as the file you are referencing it in. This answer, however, will work as a solution.

Default folder setup for a large project

As I am trying to learn C++ I have begun on a larger project where I try to use classes in order to avoid a messy main.cpp file. This means that I am creating more .cpp files which I have placed in the same folder as the main.cpp file. This has resulted in a messy directory aswell so I tried managing my files by adding folders following the two links:
Link1: https://hiltmon.com/blog/2013/07/03/a-simple-c-plus-plus-project-structure/
Link2: https://mariuszbartosik.com/directory-structure-for-a-c-project/
My questions are the following:
Does a standard for creating C++ projects exist that is used in a
workplace or is every project subjectively created?
If a standard does not exist are there any bad practises that should
be avoided when creating a folder structure?
Can I create a folder structure where I put all the header (.h)
files in one directory and all the source files (.cpp) in another
directory such as C:\headers\header.h and C:\source\main.cpp to
in my include use a #define HEADER "/path to header" and then
somehow #include HEADER "aheader.h" which would mean that I dont
need to everytime when including a header write the path to the
header directory and instead write HEADER before the include?
Example:
Instead of:
#include "c:\headers\header.h"
#include "c:\headers\anotherheader.h"
Use:
#define HEADER "c:\headers\"
#include HEADER "header.h"
#include HEADER "anotherheader.h"
I am asking this because I would like to avoid all bad practise when learning to code in C++ so I wont do mistakes later on. Since I have no work experience I dont know if the guides I found online are good ones in practise.
My current structure:
Does a standard for creating C++ projects exist that is used in a
workplace or is every project subjectively created?
No, you can find different structures across different projects.
If a standard does not exist are there any bad practices that should
be avoided when creating a folder structure?
You should:
Put your compiled files in a different folder (bin maybe).
Organize your project in different logical modules placed in different folders.
Use the root folder for not-code files (makefile, gitignore, etc...).
Use lowercase names only to avoid silly mistakes.
You should not:
Use absolute paths.
Can I create a folder structure where I put all the header (.h)
files in one directory and all the source files (.cpp) in another directory such as C:\headers\header.h and C:\source\main.cpp to in my include use a #define HEADER "/path to header" and then somehow #include HEADER "aheader.h" which would mean that I dont need to everytime when including a header write the path to the header directory and instead write HEADER before the include?
No, that's really bad.
I don't like use different folders for source/header files, but if you want to. You can work around using include paths (-l flag).
So you would use:
#include <header.h>
#include <anotherheader.h>
That works because you are including the folder on compile time:
g++ -l "../headers" enemy.cpp
Any sensible IDE will do this for you. Or you could do it on your makefile, whatever suits you.

Can you create multiple header files for a DLL? Can you consolidate header files?

Background: I'm using Project Euler as an excuse to test various C++ features and find holes in my knowledge. I have a project that creates an executable which solves various math problems. It's getting unwieldy and I'm splitting the problems out into separate executables. A lot of the math functions are re-used in several of the problems.
First: I want to create a DLL file with math helper functions. Rather than provide one single mathhelp.h file for the entire dll, I'd prefer to have several separate .h files e.g. primes.h, cartesian.h etc./ that each provide access to separate namespaces in the dll.
Is this possible?
Second: Separately, I do want a single mathhelp.h file for those projects big enough they just need access to every helper function without including fifty separate headers. How to do this is answered in:
How to export multiple header files as a single header file in C++?
but that answer gives me a follow-up question: If, in an executable project, I include the following header from the DLL project:
#ifndef MATHHELP_H
#define MATHHELP_H
#include "primes.h"
#include "cartesian.h"
#endif
...how does that compile if I don't also have primes.h in the executable project? Won't the pre-compiler reach the first #include and choke on the fact that I don't have primes.h in the project?
When you provide a c++ compiled API to a user ( let's say it is compiled in a .dll file) you should provide beside the .dll file also the .h files (containg all header that were used to compile that .dll) and .libfiles ( containg the exported symbols from that .dll ).
Now let's say that your user has the executable a.exe, and wants to use your compiled API. Usually you will provide them a file structure which looks more or less like that :
include / primes.h
cartesian.h
mathhelp.h
lib / Api.lib
bin / Api.dll
When an external executable uses your code he will need to add external flags in the compilation procedure like : -I/pathToInclude -L/PathToLib -l/yourLibrary ( you need to check the exact sintax for doing that).
In your code you you will have something like this:
#ifndef MATHHELP_H
#define MATHHELP_H
#include <primes.h>
#include <cartesian.h>
#endif
In this case the compiler will look firstly in the current directory ( where he won't find the headers) and then in the folders specified by the -I flag ( where he will find your headers).

How to make header files can be include via Library name?

I'm trying to make a cross-platforms crypto library in C++ at https://github.com/haithngn/cryptor something like https://github.com/MailCore/mailcore2
The Structure is:
Can I make any header files can be include in the statements like this:
#include <Cryptor/MD5Encryptor.h>
I can include these header directly from absolutely file path
../core/CryptorCore.h
But these format will make the source code cannot build succeed in an XCode Project.
I tried to simulate the MailCore2 but that's very difficult.
Hope you can suggest me any solution or do a favor PR on my Repository.
Thanks,
You need to have a proper hierarchy. First, no, you can't have
#include <Cryptor/MD5Encryptor.h>
with your current setup, not while building the library, and not without flattening the hierarchy when installing your files (which CMake can do).
What you can do is:
#include <Cryptor/core/abstract/MD5Encryptor.h>
if you add your project inside a Cryptor folder instead of being at the root of your project.
I would advise you to create a Cryptor.cmake file that would allow people to pick up your library once installed, so that they don't have to know where the library includes are or what the libraries name is.
It should not be necessary to point to every single header file. Just add all directories containing these header files with include_directories(PATH_TO_HEADERS).
For the include of the header file src/core/CryptorCore.h with
#include "CryptorCore.h"
you have to point to
include_directories(${PROJECT_DIR}/core/)

how can creat MFC extention dll with multi class?

I want to create a MFC extention dll, my problem is the class that I want use in client include some header file that implemented in my project, now how can use it in client?
For example the header file class that want to export from my dll is like below
#include "classme1.h"
#include "classme2.h"
class AFX_EXT_CLASS dllclass
{
...
//anthor function
};
When I include this header file in client, the client project has error and need two file "classme1.h" and "classme2.h".
What I do to solve my problem?
Copy the header files in a common place were both projects can read them.
You can define the path in the #include statement, or you can change the project settings for the C++ pre processor to search the files in different additional directories.
Project settings -> C/C++ -> General -> Additional Include Directories.
For complex projects I very often use the Property Manager. You can define specific settings and add them to your current project. It is just a file that allows you easily to customize projects.
Remember that you also need a reference to the LIB file. It must bes specified in the linker section. Or must be added to the project too. (see tip above).
Alternative to xMRi's suggestion is to refactor your extension DLL to remove the need for a client to include your private header files.
Read about PIMPL Idiom (Pointer to IMPLementation), for example here: Why should the "PIMPL" idiom be used?