How to test a custom module in MSVC's googletest - c++

My project solution is roughly like this:
Project("{xxxxxxxxxxxxxxxx11111111}") = "project1", "project1.vcxproj", "{xxxxxxxxxxxxxxxx222222222222222}"
EndProject
Project("{xxxxxxxxxxxxxxxx11111111}") = "test", "test\test.vcxproj", "{xxxxxxxxxxxxxxxx333333333333333}"
EndProject
Project brief description:
I have two projects, one is the main project, the directory is project/src/balabala.ixx, and the other is the added googletest project, the directory is project/test/balabala.cpp
My requirement is to test whether the module of my main project meets my needs in the sub-project of googletest, but I can't use the module of the main project now, VS tells me that it can't find my module.
like this:
#include "gtest/gtest.h"
import std.core;
// cannot find my module
// import myproject.some_module;
TEST(TestCaseName, TestName)
{
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
// it works
std::cout << "hello world";
}
I also looked for Microsoft's documentation, and I found
https://devblogs.microsoft.com/cppblog/a-tour-of-cpp-modules-in-visual-studio/#external-modules
But it did not tell how to set the path, no matter the path I filled in is ../ or absolute path VS said that the target file cannot be opened
VC++ Directories -> all module is public -> true also not work :(
==========================================================================
After various attempts to no avail, I decided not to use the subproject. I copied the original include and lib folders of googletest to project/3rd-party/googletest, and then set up additional include directories and additional lib directories.
then I write this:
// project/test/test.cpp
#include <gtest/gtest.h>
// it found my module!!!
import muproject.some_module;
TEST(TestCaseName, TestName)
{
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
}
But when I choose to run the test case, the compiler outputs the following:
Build started...
1>------ Build started: Project: my_project, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>test.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\sstream(262,1): error C2270: '()': modifiers not allowed on nonmember functions
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\sstream(237): message : while compiling class template member function 'int std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char>>::overflow(int)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\sstream(905): message : see reference to class template instantiation 'std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char>>' being compiled
1>`path_to_my_project`\3rd-party\googletest\include\gtest\gtest-message.h(232): message : see reference to class template instantiation 'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>' being compiled
1>d:\a01\_work\3\s\binaries\x86ret\inc\compare(336,31): error C2028: struct/union member must be inside a struct/union
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\sstream(262,1): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\HostX86\x64\CL.exe'
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>Done building project "my_project.vcxproj" -- FAILED.
I double-checked the settings (x64/x86/debug/release) and confirmed that the lib settings are correct
gtest-message.h(232): message : see reference to class template instantiation 'std::basic_stringstream<char,std::char_traits,std::allocator>' being compiled
// We declare (but don't implement) this to prevent the compiler
// from implementing the assignment operator.
void operator=(const Message&);
// -> this line is line232
========================================================================
It's too strange, this seems to be a probabilistic event. If you don't import std.core, the compilation will generally pass, but once std.core is imported, it will most likely fail to compile (the error is as above) (but occasionally it can be compiled)

Related

How to get Modules to work in VS2019 16.8?

I'm trying to convert my program to modules. I'm using MSVS 19 Community Edition version 16.8 (also tried with MSVS19CE Version 16.9.01 Preview)
Each .ixx IDF I write compiles fine when compiled indiviually, (with "import"'s and its usage commented out),
Build started...
1>------ Build started: Project: myCare, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>Compiling...
1>DB.ixx
1>Compiling...
1>HS.ixx
1>pch.cpp
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
But when I introduce the commented out imports, suddenly VS can't find the modules it just previously compiled beautifully.
Build started...
1>------ Build started: Project: myCare, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>Compiling...
1>HS.ixx <<<<----- Compiled without errors
1>Compiling...
...
1>ComplexComboBox.ixx
1>D:\Dev\myCare\Modules\ComplexComboBox.ixx(289,1): error C2230: could not find module 'HS'
1>D:\Dev\myCare\Modules\ComplexComboBox.ixx(289,1): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x64\CL.exe'
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>Done building project "myCare.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
error C2230: could not find module 'HS'
error C2230: could not find module 'Aui'
error C2230: could not find module 'Ctrl.ListCtrl'
Any hints? Every module has the structure like this (and has the file extension .ixx)
module;
#include "Defines.h"
#include <any>
#include <bitset>
...
export module HS;
namespace HS
{
export enum class Comms { None = 0, Port = 1, Timeout = 2, Authority = 3, Security = 4 };
export auto CommsAuthority(const AuthorityType newValue)->void;
export auto CommsAuthority(void)->AuthorityType;
(Not everything in here is exported)
...
}
module :private;
auto HS::CommsAuthority(const AuthorityType newValue)->void
{
...
}
auto HS::CommsAuthority(void)->AuthorityType
{
...
}
...
This seems to be a bug with the way visual studio currently handles modules imported by other modules. Essentially when each module compiles it creates a <module_file_name>.ixx.ifc file. This is detected perfectly fine when the module is imported into a cpp file but not when importing to a module. removing the .ixx from the filename will import correctly whether importing to another module or a cpp file though.
I solved this in 1 of my own projects through the project properties(the required options are only available in the preview version of visual studio at the moment though).
So you first need to set Module File name to $(IntDir)%(Filename).ifc in C/C++->Output Files
Then set Additional BMI Directories to $(IntDir) in C/C++->General.
Visual studio puts all the intermediate module stuff in the intermediate directory by default but if you'd rather have all your intermediate module things in a separate folder you can set Module Dependencies File in C/C++->Advanced to wherever you want and then update the $(IntDir) bits above to the new location.

VS2019:fatal error C1083 Cannot open header file: 'opencv.hpp'

It is all fine when I was writing the code, that is, when I included the header file "opencv2/opencv.hpp",and VS2019 can indeed "see" it,in other words, I can use the class that is in the opencv.hpp, but only when I complied the project,c1083 occurred
before i added the function DrawLine(),i compiled for times,with no err.and then i added the function DrawLine(..),error occurred
this is the total source code
a.ha.cpp
//a.h
#include<opencv2\opencv.hpp>
using namespace cv;
class Canvas{
Mat mat_canvas;
void Init(){
mat_canvas.create(1024,1024,CV_32FC3);
}
void DrawLine(float ax,float ay,float bx,float by);
}
//a.cpp
#include"a.h"
void Canvas::DrawLine(float ax,float ay,float bx,float by){
cv::line(this->mat_canvas,Point2f(ax,ax),Point2f(bx,by),Scalar(1,1,1));
}
error msg was:
fatal error C1083 Cannot open header file: 'opencv.hpp': no such file or dir
i know i can create a new solution and move my old solution to it,but it takes time,and i can't actually know whether this issue will happen again in my new solution
Any answer will be helpful
Actually there are most of the time possibility of missing "Additional include directory path" or wrongly setup.
Where to find in Visual Studio:
To set this compiler option in the Visual Studio development environment
Open the project's Property Pages dialog box. For details, see Set
C++ compiler and build properties in Visual Studio.
Select the Configuration Properties > C/C++ > General property page.
Modify the Additional Include Directories property.
Visual Studio 2019 C++ Project configuration
Example project setup is described in below link:
Example setup

Visual C++ : XGBoost does not work when called from a DLL

I have a requirement to use XGBoost within a Visual C++ project DLL.
I have been able to download and build the XGBoost source using VC++ and CMake.
When I include the XGBoost code in a test console application, it works fine.
However, when I try to replicate what I've done with the console application in a DLL project, it won't compile.
I am struggling to even use a basic XGBoost type within the project.
I suspect the problem is my ignorance with DLL projects and would appreciate your help.
Here's what's happening in my DLL project:
When I use the following include as the very first line in a cpp class file, it compiles:
#include <xgboost\c_api.h>
With it compiling, if I try to use a simple type defined in this include file, the build fails with the following message:
...\dll_test\xgb_driver.cpp(20): error C2065: 'BoosterHandle': undeclared identifier
This is the line that causes the error:
BoosterHandle my_handle;
"BoosterHandle" is in fact defined in <xgboost\c_api.h>
When I put the include below any other include, I get the following error messages:
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C2146: syntax error: missing ';' before identifier 'bst_ulong'
Below is a little more information on what I did to get XGBoost working with a console app and how I created the DLL project.
=-=-=-=-=-=
Here's what I did to use XGBoost with a console. Everything about it seems to work. I've tested the model predictions, and they are
consistent with what I'm seeing in R.
Using the documentation found here:
https://xgboost.readthedocs.io/en/latest/build.html
I downloaded the XGBoost source and built it using CMake and Visual Studio 2015.
Under Project > Linker > Input > Additional Dependencies,
I added the xgboost.lib file
Under Project > Linker > General > Additional Library Directories
I added a reference to ...\xgboost\build\Release
Under Project > VC++ Directories > Include Directories
I added the path to ...\xgboost\rabit\include and ...\xgboost\include
I put the xgboost.dll in the directory where the .exe is generated.
From here, it was smooth sailing.
=-=-=-=-=-=
Here's what I've done to create a Visual C++ DLL Project:
After choosing File > New > Project, I select an ATL DLL Project (this is part of the project requirement).
For the XGBoost include to the project, I repeated steps 1-5 above, but placed the xgboost.dll file where the
project DLL would be generated.
Here is the source for the header file for the simple class I have created:
#pragma once
class XGB_Driver
{
public:
XGB_Driver();
~XGB_Driver();
float callXGB(float sample_input);
};
Here is the source for the simple cpp file:
#include <xgboost/c_api.h>
#include "stdafx.h"
#include "XGB_Driver.h"
XGB_Driver::XGB_Driver()
{
}
XGB_Driver::~XGB_Driver()
{
}
float XGB_Driver::callXGB(float simple_input) {
BoosterHandle my_handle;
return(0);
}
Why this doesn't work for the ATL DLL project, but does for the console app really has me banging my head against the keyboard.
Your help would be very much appreciated.
Best,
Dave
With the help of CristiFati, I believe this question has been answered.
For whatever reason, it seems that using XGBoost with C++ in a DLL project requires additional includes above and beyond what is required for using it in a console application.
I am currently building the DLL project with the addition of:
#include <cstdint>

Cannot run Hello World C++ program

I am just starting C++ and downloaded a compiler and an IDE, both eclipse, and tried to make my first C++ program. I use the Hello World C++ Makefile Project, and add the all the stuff on the next page. I then build the program, and the build says this:
12:30:00 **** Build of configuration Default for project HelloWorld! **** make all Cannot run program "make": Launching failed
Error: Program "make" not found in PATH PATH=[C:/Program Files
(x86)/Java/jre1.8.0_91/bin/client;C:/Program Files
(x86)/Java/jre1.8.0_91/bin;C:/Program Files
(x86)/Java/jre1.8.0_91/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Program
Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files
(x86)\Common Files\Microsoft Shared\Windows
Live;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;c:\Program
Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files
(x86)\Common Files\Roxio Shared\DLLShared\;c:\Program Files
(x86)\Common Files\Roxio Shared\12.0\DLLShared\;C:\Program Files
(x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL
Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL
Server\100\DTS\Binn\;C:\Users\Jen\Desktop\eclipse;] 12:30:00 Build
Finished (took 122ms)***
I run the program, and it says:
Launch failed. Binary not found.
This is the code for the program:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello World!");
return EXIT_SUCCESS;
}
There are two errors, too:
Function "puts" couldn't be resolved.
Symbol "EXIT_SUCCESS" couldn't be resolved.
Thanks in advance!
Nate N.
/////////////////////////////////////////////////////////////////////////
I followed the advise of user4581301 and the code now says this:
#include <iostream>
using namespace std;
int main() {
cout << "Hi World" << endl; // prints Hi World
return 0;
}
The build looks like this:
13:22:26 **** Rebuild of configuration Debug for project HiWorld ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\HiWorld.o" "..\src\HiWorld.cpp"
g++ -o HiWorld.exe "src\HiWorld.o"
13:22:27 Build Finished (took 835ms)
But now the program, although it does run, doesn't print anything.
Thanks in advance,
Nate N.
Launch failed. Binary not found.
well this is shown because you have 2 errors.
any error means = no binary compiled/linked
no binary compiled/linked = cant launch something which doesnt exist
solution = fixing the errors
#include #include
^ also this is not valid
Cannot run program "make": Launching failed Error: Program "make" not found in PATH
Kaboom! No make, no build, no binary to run.
Some compilers don't ship with Make out of the box, but I don't see any signs of a compiler toolchain in your path at all.
Eclipse is an IDE with support for a wide variety of different compilers. as a result it does not ship with one. Since the error message shows you are building on Windows, your simplest option is one of the many MinGW variants. And Since you are just starting, I recommend starting off with the plain-vanilla MinGW as Eclipse requires next to no extra configuration to use it.
Just make sure you follow the instructions to install msys along with MinGW to get make.

How to force MIDL compiler to use "C++ mode" instead of "C mode" in Visual Studio 2010?

My problem is simple, but I don't know how to resolve it: I have a Visual Studio 2010 project with idl files for generating COM type library. This project was originally created by VC++ 6.0. The VC 6.0 could compile this project without any error. But I converted it recently and keep trying to compile it in VC 2010. I'm allways getting the following console output message:
1>------ Build started: Project: myProject, Configuration: Debug Win32 ------
1>Build started 08.04.2013 11:23:12.
1>InitializeBuildStatus:
1> Touching ".\Debug\myProject.unsuccessfulbuild".
1>Midl:
1> Processing .\myFile.idl
1> myProject.idl
1>C:\Programme\Microsoft Visual Studio 10.0\VC\include\typeinfo(29): fatal error C1189: #error : This header requires a C++ compiler ...
1>
1>midl : command line error MIDL1003: error returned by the C preprocessor (2)
1>
1>Build FAILED.
I've already figured out that this does only mean, that the MIDL invokes the compiler in "C mode", but not in "C++ mode". The project contains neither .c nor .cpp, has only .h and .idl files: C/C++ section is NOT available at project property pages. I tried to add a dummy.cpp file, to make that missing section available and to be able to default the C++ compiler in it.
The Midl file looks like:
import "unknwn.idl";
//Globale Definitionen
#include "MyProjectGlobaleDefinitionen.h"
//Interfacedeklarationen
interface IMyProjectDcomSchnittstellen;
[
uuid(E6C14CB3-CFFE-11d4-B1A2-00104BC1A971),
helpstring(MyProject_SCHNITTSTELLE_RELEASE_2_HELPSTRING),
version (MyProject_SCHNITTSTELLE_VERSIONSNUMMER)
]
library MyProject_SCHNITTSTELLE_KURZBEZEICHNER
{
importlib("..\\..\\..\\Files\\DLL\\stdole32.tlb");
importlib("..\\..\\..\\Files\\DLL\\MSADO15.dll");
// #include "..\..\common\Hresult_Werte.h"
#include "MyProjectDcomSchnittstellenTypdefinitionen.idl"
#include "IObj1ManagerMyProject.idl"
#include "IObj1ManagerMyProjectEvents.idl"
#include "IObj2Manager.idl"
#include "IObj2NotifySink.idl"
//************ Obj2Manager **************************
[uuid(F0284CF2-8E24-11d4-8941-006097AA387A)]
coclass Obj2Manager
{
[default] interface IObj2Manager;
[default, source] interface IObj2NotifySinkEvents;
}
//************ Obj1ManagerMyProject ************************
[uuid(E6C14CB2-CFFE-11d4-B1A2-00104BC1A971)]
coclass Obj1ManagerMyProject
{
[default] interface IObj1ManagerMyProject;
[default, source] interface IObj1ManagerMyProjectEvents;
}
}; //Ende library Obj1_myProject
How can I force the MIDL to invoke the compiler/preprocessor in "C++ mode"?
Shouldn't this
#include "MyProjectDcomSchnittstellenTypdefinitionen.idl"
#include "IObj1ManagerMyProject.idl"
#include "IObj1ManagerMyProjectEvents.idl"
#include "IObj2Manager.idl"
#include "IObj2NotifySink.idl"
actually be this
import "MyProjectDcomSchnittstellenTypdefinitionen.idl"
import "IObj1ManagerMyProject.idl"
import "IObj1ManagerMyProjectEvents.idl"
import "IObj2Manager.idl"
import "IObj2NotifySink.idl"
Okay... How about this one here:
This page applies only to developers who have specific reasons to replace the Microsoft C/C++ preprocessor as the preprocessor used by MIDL, or to developers who must specify customized preprocessor switches. The MIDL switches /cpp_cmd, /cpp_opt, and /no_cpp are used to override the default behavior of the compiler. There is typically no reason to replace the Microsoft C/C++ preprocessor, nor to specify customized preprocessor switches.
The MIDL compiler uses a C preprocessor during initial processing of the IDL file. The build environment used when compiling the IDL files is associated with a default C/C++ preprocessor. If a different preprocessor is to be used, the MIDL compiler switch /cpp_cmd enables an override of the default C/C++-preprocessor name:
midl /cpp_cmd preprocessor_name filename
In the include file MyProjectGlobaleDefinitionen.h there was an include present. I had only to remove this include, because it was not needed at all.