Including code to be compiled only if static constexpr is met C++ - c++

Is it possible to include code to be compiled only if a static constexpr has a certain value?
Take this for instance
static constexpr auto VERSION_MIN = 123;
If the number were set to 124 include the code to be compiled otherwise exclude it.
Basically I have two source packages which are identical except for a few lines of code which are considered extra or a minor difference.
I just want to make a universal application where I don't need to recompile to switch versions.
How would I check to see if a constexpr is equal to 124, would I just use a basic control structure? Or is there another way to do this?

The following works with gcc 4.9:
static constexpr auto VERSION_MIN = 123;
void myFunction()
{
if (VERSION_MIN == 123) {
printf("This is version 123\n");
}
else {
printf("This is another version\n");
}
}
On Linux (don't now such tools for Win) you can check that the binary does not contain the string "This is another version\n".
Thus you can replace
#ifdef VERSION_MIN 123
printf("This is version 123\n");
#else
printf("This is another version\n");
#endif
My IDE (QtCreator) handle the "pure" C++ code better than the preprocessor code.

Microsoft Visual Studio 2015 (If you are using it) has solved this problem with Build Configurations. You can create a new Build Configuration and then give it a preprocessor directive to insert automatically into the code when it builds. This will allow you to create a different Build Configuration for each 'Version' of your code and then you can 'batch build' to build each version; without changing the actual source code for each version.
To do this go to Configuration Manager (Under Release/Debug Dropdown) then then give your Version a name and copy settings from your previous building configuration. After you have created a Build Configuration select it in the Configuration drop down to make it active and then go to Project Properties (Alt+p+p) -> C/C++ -> Preprocessor -> Preprocessor Definitions and add a preprocessor to control what code is compiled in that version and what isn't.
#ifdef _DEBUG
std::cout << "data data data" << std::endl;
#endif //_DEBUG

Related

C++Builder11: How to unit test with googletest?

Until recently I've used C++Builder 10.2 for a project, and I had begun to use DUnitX to add some unit tests for the project.
Now I have upgraded to C++Builder 11.2, and found that DunitX is no longer supported for C++Builder when using this version. Instead, Embarcadero recommends to use DUnit or Googletest.
On further research, it seems that Googletest cannot be used with the classic compiler (but I'm not actually interested in using the pre-C++11 classic compiler), but also that DUnit cannot be used when targeting the Firemonkey framework, and that DUnit (1) is unmaintained and (2) does not work well with the Clang-based compiler.
I'm interested in using googletest because I have already used both, googletest and googlemock, on less niche platforms than C++Builder such as Linux/GCC, Apple/Clang and Windows/MinGW-w64. I am aware that the googletest project itself refuses to accept build files or patches for C++Builder because they do not want to spend effort to support niche compilers (see e.g. here, here, and here).
I'm happy to learn that some patched version of googletest is currently available for C++Builder through the GetIt package manager, even though it is not clear who has actually made that patch, and although I realize that Embarcadero may remove googletest from the GetIt package manager an any moment.
I've found two blog posts explaining how to install googletest in C++Builder and how to use it, however, I cannot successfully follow the second blog post when it comes to point 6, which reads
In your project group create a new windows64 bit VCL console application. Set this to use the debug settings (this allows you to debug code that doesn’t pass a unit test).As well as the files you want to test and the files containing the testing code you need to add to the project the library file …GT2021.09\cbuilder\lib\Win64\Debug\gtest.a.
I'm not sure how I am supposed to "add to the project the library file". I've tried to
copy the gtest.a and gmock.a files into the project directory and then
right-click on the project name in the "Projects" view of the IDE and select "Add...", then change file type to "static libraries", then select gtest.a and repeat with gmock.a.
Here I've gone ahead and have already added gmock.a because I have experience with googlemock and envision that its additional matchers and mock class generators will help me writing tests.
When I compile a simple test project that does not actually perform any tests, everything compiles and links fine, but when I execute the resulting command line program, then it fails with exit code (errorlevel) -1073741819 and produces no output. This does not happen if I comment all usages of googletest out.
The simple test project which fails during execution consists only of
#include <gtest/gtest.h>
#include <vcl.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
which should cause googletest to print that 0 tests were executed, but instead it crashes as described. When replacing the two lines in main with a simple printf, which does not use googletest, but leaving all includes unaltered and without altering the project with regard to libraries, it works fine (the new printf prints something) but of course cannot perform any tests.
How to fix this?
One more observation: When adding the static libraries to the project as described above, I get a notification message box from the IDE, saying "One or multiple lines were too long and have been truncated". I have no idea how this message could make sense with regard to adding a static library to the project. It seems however, that this is not an error, and the linker actually uses the static libraries when linking.
The main problem here was the inclusion of the gmock.a library as it was compiled by the GetIt googletest package. This gmock project and basically all other gmock projects in the GetIt package are broken and need to be repaired before using them. I may post more details about this in a future topic. The gmock.cbproj project as distributed by GetIt, e.g., includes the unrelated source file googletest\samples\sample8_unittest.cc, among other errors.
A simple method to use googletest with C++Builder 11.2, which is based on the blog posts by Cigol, but which does not require to copy include files and library files:
When installing googletest with the getit package manager, the IDE automatically opens a group project Googletest.groupproj and compiles two of the contained projects (gtest and gtest_main) for the Windows 64 bit platform in "Release" mode. Furthermore, all other project files in the Googletest project group are modified probably because they have been updated from an earlier C++Builder version and want to be saved when closing the IDE.
There is no need to compile googletest in "Debug" mode, one would need that only for debugging the unit testing framework itself.
Next, create a new VCL Windows 64 bit console application to start using googletest:
File -> New -> "Console Application - C++Builder"
Source Type: C++, Target Framework: Visual Component Library, [OK]
Add Target Platform Windows 64-Bit in the "Projects" view (right-click on Target Platforms).
Delete Target Platform Windows 32-Bit.
Save all in a dedicated directory:
File -> Save All
Create a new folder, e.g. MyUnitTests.
Place project file as e.g. MyUnitTests.cbproj into that folder.
Rename File1.cpp to MyTestsMain.cpp and store in that folder
This creates a C++ source file MyTestsMain.cpp with some includes and an empty main function:
#include <vcl.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
}
For convenience, googletest provides a library gtest_main.a which only contains a main function that one can use to execute all unit tests compiled into an executable. By linking against the gtest_main.a library, users can avoid writing their own main function and concentrate on only writing test code. But since the C++Builder wizard has already created a main function, one can as well fill the generated main function with the necessary boilerplate code (only two lines are required, compare against the googletest main function in C:\Users\yourLogin\Documents\Embarcadero\Studio\22.0\CatalogRepository\GoogleTest-2021.09\googletest\src\gtest_main.cc) and add the gtest.h include directive:
#include <gtest/gtest.h>
#include <vcl.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Trying to build this project fails because the gtest/gtest.h include file is not found. This can be fixed in Project -> "Options..." -> Building -> C++ Shared Options -> "Include path": After selecting "All configurations - All platforms" in the drop-down list "Target", add the following entry to "Include path":
$(BDSCatalogRepository)\GoogleTest-2021.09\googletest\include
Using the variable $(BDSCatalogRepository) avoids machine- and developer-specific absolute PATHs. Save the changed project settings with File -> "Save all". Trying again to build this project now fails because of different errors, which is progress! The errors now are "Unresolved external"s, which means we have to tell the project to link against gtest.a and where to find it. Linking against gtest.a can be done by adding a pragma to the top of the file containing the main function:
#pragma comment(lib,"gtest")
#include <gtest/gtest.h>
#include <vcl.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Where to find the library can be configured in Project -> "Options..." -> Building -> C++ Shared Options -> "Library path": Again first select "All configurations - All Platforms", then add the following entry to "Library path":
$(BDSCatalogRepository)\GoogleTest-2021.09\cbuilder\lib\$(Platform)\Release
After File -> "Save All", a new, clean build generates different "Unresolved external"s, progress! This time, symbols from the standard C++ library are missing, which can be fixed via Project -> "Options..." -> Building -> C++ Linker, again for Target "All configurations - All platforms", check the checkbox "Link with Dynamic RTL, Windows...". After another File -> "Save All", a clean build succeeds and executing the generated Win64\Debug\MyUnitTests.exe generates this output:
[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[ PASSED ] 0 tests.
One can now add tests to the test project. Tests can be added to the source file which contains the main function or to different, topic-specific source files. I'll add two tests in new files for demonstration:
In the "Projects" view, right click on the current project, which is confusingly named "MyUnitTest.exe" in the project view with an ".exe" extension instead of a project file extension, then select "Add new..." -> Unit in the popup menu. "Unit" here is C++Builder's language for a pair of one source and one header file, and is not necessarily related to unit testing.
The new files are initially named "Unit1.cpp" and "Unit1.h" but can be renamed when doing File -> "Save All". I name this first test file to "SelfContainedTest.cpp" because its test will be self-contained. Add the following code to the .cpp file after the IDE-Generated boilerplate:
#include <gtest/gtest.h>
TEST(SelfContained, Addition) {
EXPECT_EQ(3, 1+2);
EXPECT_GT(3, 2+2);
}
Rebuilding succeeds, execution reveals that the second EXPECT fails as it should, the number 3 is in fact not greater than the sum 2+2. Fix if you like.
In a second test, I want to test non-GUI methods of an existing VCL form. In a real-world scenario, the GUI project and my test project would be part of the same project group and live in the same directory or below the same parent directory, and I would add the VCL form's .cpp file also to the test project with (Project View) -> right click -> "Add..." -> C++Builder unit (*.cpp). My form TAdderForm that I'm using here is a simple form with two VCL TEdit fields for entering numbers and a VCL TLabel to display the sum of the two numbers. The sum is computed in a method
int TAdderForm::add(int num1, int num2)
{
return num1 + num2;
}
which I want to test here. To write the test, I add a new "Unit" to the test project as before, naming the source file "VCLTest.cpp" this time. After the IDE-generated boilerplate, I add this code to the .cpp file:
#include "adderFormx.h"
#include <gtest/gtest.h>
TEST(VCL, Addition) {
// Have to instantiate VCL form before calling its method.
Application->CreateForm(__classid(TAdderForm), &AdderForm);
EXPECT_EQ(3, AdderForm->add(1,2));
EXPECT_GT(3, AdderForm->add(2,2));
delete AdderForm; // Delete no longer used form.
AdderForm = nullptr; // clear pointer, another test may allocate new instance
}
This is basically the same test as before. The second expectation will fail again and needs to be fixed because 3>4 is a wrong expectation. The test uses the global instance pointer "AdderForm" from the form's source file for simplicity, this can be modified if required. If multiple tests want to instantiate the same form, a fixture should be used and the setup and teardown done here inside the test should be moved to the fixture's respective methods, but this is no longer C++Builder specific.
Note that Application->Run() is never called, and no GUI elements actually appear on the screen when executing the tests. I'm restricting tests to non-GUI methods of the GUI classes.

Code coverage error (No binaries were instrumented)

Problem:
Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
I'm attempting to use the code coverage function but it returns this error:
Empty results generated: No binaries were instrumented. Make sure the
tests ran, required binaries were loaded, had matching symbol files,
and were not excluded through custom settings. For more information
see http://go.microsoft.com/fwlink/?LinkID=253731
My .coverage file is full of non-sense, starting with "PCHÿ". I thought my problem looked similar to this one: Issue with Code Coverage in VS 2012, but deleting the .suo file and running Visual Studio in admnistrator didn't solve anything.
How to replicate:
1) Create new empty project "MyProject"
2) Add new file "Calculator.hpp"
#pragma once
class Calculator
{
public:
int add(int a, int b);
int sub(int a, int b);
};
3) Add new file "Calculator.cpp"
#include "Calculator.hpp"
int Calculator::add(int a, int b)
{
return a + b;
}
int Calculator::sub(int a, int b)
{
return a - b;
}
4) Add new file main.cpp
#include "Calculator.hpp"
#include <iostream>
using std::cout;
int main()
{
Calculator calc;
std::cout << calc.add(5, 11) << std::endl;
std::cout << calc.add(11, 1) << std::endl;
return 1;
}
5) Build solution. (0 errors)
6) Add new test project to solution as explained here: https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef:
Add new Native Unit Test Project to solution "CalculatorUnitTest"
Add "main.obj" and "Calculator.obj" to Linker/Input/Additional Dependencies of "CalculatorUnitTest"
Add path to obj files to Linker/General/Additional Library Directories of "CalculatorUnitTest". (In my case: D:\JF\Programming\Tests\MyProject\MyProject\Debug)
Add the header directory of "MyProject" to VC++ Directories/Include Directories. (In my case: D:\JF\Programming\Tests\MyProject\MyProject).
7) Replace code in "unittest1.cpp" with:
#include "stdafx.h"
#include "CppUnitTest.h"
#include <Calculator.hpp>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace CalculatorUnitTest
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
Calculator calc;
Assert::AreEqual(5, calc.add(2, 3));
}
};
}
8) Build solution (0 errors, TestMethod1 appear in Test Explorer)
9) Right-click TestMethod1, "Run Selected Tests" (Test successfully passes)
10) Right-click TestMethod1, "Analyze Code Coverage for Selected Tests" (Code coverage fails).
At D:\JF\Programming\Tests\MyProject\Debug, I have both my MyProject.exe and MyProject.pdb with the CalculatorUnitTest.dll.
First of all, I am not sure if Visual Studio 2015 (C++) supports ".exe" type project for the unit test. But you are using ".obj" directly from your project. You need to place ".pdb" file in the same folder.
I created a static library project and added the unit test. Same problem on my environment.
Future here. Same problem, except my test target is a Static Library project in the same Solution.
MyLib - Code that will be tested.
MyLibTests - Project containing the tests.
Solution contains both projects.
MyLibTests has project Reference to MyLib.
<OutDir> (in .vcxproj) aka Output Directory aka $(OutDir) identical in both projects. (My value <OutDir>$(SolutionDir)$(Configuration)_$(Platform)\</OutDir>)
On your test project (MyLibTests), go
Properties
Select [Debug|platform]
Linker
Debugging
Generate Debug Info = Optimize for debugging /DEBUG
Mine was initially set for /DEBUG:FASTLINK.
Via this social MSDN post I figured it had something to do with the PDB, because MSDN (aka now "learn.microsoft.com") said this is an important step in several places, and I assume PDB are used trace calls, I assume using DbgHelp.dll.
And this sort of checklist modified from another social MSDN post:
Test project References properly set (MyLib.lib is in the same Solution as MyLibTests.dll)
Build order/dependency set/verified.
I also have Autorun on Build.
Rebuild (this should rebuild dependencies).
made sure all test were run(all test run successful)
not using optimisation (Profile Guided/Instrumentation etc).
<OutDir> aka Ouput directory should be the same, where both MyLibrary.lib/dll/exe ends up with their MyLibrary.pdb since PDB paths are linked in the exe/dll.
not using a runsettings file
Unload all other projects other than affected ones

Bug in Xcode 7 about precompiled header and forward declaration in C++

I found a bug in latest Xcode 7.0 that annoys us very much in our company because it makes most of our C++ code not debuggable. After lots of experiment, I was able to reproduce it with a minimum amount of code.
In some cases, it is impossible to see members inside a C++ class on LLDB. It seems that three conditions must be present for that bug to appear:
the class is forward declared
the class has a virtual method
the class is declared inside a precompiled header
I am asking whether somebody else already knowns about that bug and what is the recommended procedure to report that bug (to LLVM or Apple ?).
Steps to reproduce:
Create two source files with their content:
header.h
#ifndef HEADER_INCLUDED
#define HEADER_INCLUDED
class A; // forward declaration, has an effect on bug
class A
{
public :
virtual ~A() {}
protected:
int doYouSeeMe;
};
#endif
PCHAndFDbug.cpp
#include "header.h"
int main()
{
A* a = new A();
return 0;
}
Create a small Xcode 7 project with these two files. header.h must be set as a precompiled header (Prefix header setting in Xcode). As a reference, I am using Premake to generate that project and here is the premake5.lua source:
solution "PCHAndFDbug"
configurations {"Debug"}
xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = "10.7" }
project "WithPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
pchheader "header.h"
project "WithoutPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
Place a breakpoint on the return 0 statement. Check if you can see member doYouSeeMe in a variable.
Same issue for me. Fixed by turning off "Enable Clang Module Debugging" in the Build Settings
For Apple at least, you should submit a bug report through the developer centre https://developer.apple.com/bug-reporting/
I've encountered this bug too and it is annoying.

Compile Maxmind C library with visual studio 2010

does anybony compiled successfully the Maxmind C library on Visual Studio 2010? I'm not able to compile it on Windows because I get a lot of errors regarding files not found like unistd.h
The error that you are seeing is probably because you are including GeoIPUpdate which isn't really necessary. GeoIPUpdate is a standalone script for updating the databases, and isn't necessary to use the APIs themselves. Try removing that to see if that resolves your issues.
Additionally, to get version 1.4.8 to compile for me on Visual Studio 2005 I had to do the following additional steps:
In GeoIPCity.c change the inclusion of the GeoIP*.h files to use "" instead of <>
In GeoIPCity.h change the inclusion of GeoIP.h to use "" instead of <>
In GeoIP.h add #define ssize_t long
In GeoIP.c change PACKAGE_VERSION to "1.4.8"
In GeoIPCity.c you cannot use a static const when declaring an array. Change the definition of tmp_fixed_record to be
unsigned char tmp_fixed_record[6+4]; //Can't use CITYCONFIDENCEDIST_FIXED_RECORD in declaration
In GeoIPCity.c declare t at the start of _extract_record().
Add main function to GeoIPCity.c to get your code to compile.
Download the zlib125.dll.zip files from http://www.winimage.com/zLibDll/index.html. Extract these files and save the dllx64/* and static64/zlibstat.lib files to a location on disk.
Now, in Visual Studio go to Project->Properties->Linker->Input and under "Additional Dependencies" add "ws2_32.lib zlibwapi.lib zlibstat.lib". Next, Under Linker->General go to
"Additional Library Dependencies" and add the location of where you saved the files above.
In GeoIPCity.c and GeoIP.c pread is undefined. Add the following definition to each of these files:
#define pread my_pread
static size_t my_pread( int file_no, void *buffer, size_t size, size_t offset )
{
if (_lseek( file_no, (long)offset, SEEK_SET) == offset)
return _read(file_no, buffer, (int)size);
else
return -1L;
}
Additionally, add #include <io.h> to GeoIP.h so that _lseek and _read are included.

Disassembling Simple Hello World program

I wrote this small C++ program and built it(Release)
#include<iostream>
int main(){
std::cout<<"Hello World";
return 0;
}
When I disassemble it, it has a lot of extra code(security cookie etc..). I believe Visual Studio is adding all those. How can I compile this program without any extra information, so that its easy to understand its disassembled code?
I know assembly is comparatively harder, but what I mean is getting a hello world asm code out of a hello world c++ program. Is this possible?
You're starting with a huge code base with <iostream>. What you might want to do is avoid the use of a runtime library entirely. Try something like this:
#include <windows.h>
int main() {
HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);
WriteFile(stdout, "Hello world\n", 12, NULL, NULL);
return 0;
}
Compile that with assembly listing turned on, and that should give you some "raw" Win32 code to start with.
you can generate assembly output in Project Properties -> Configuration Properties -> Output Files -> Assembler Output
This will let you see the assembly for the code you wrote.
Diassembling, you are going to get a bunch of other things that are linked in.
To control visual studio code generation features, rigth click on your project in VS -> properties -> Configuration properties -> c/c++ -> code generation.
Don't forget to select the right build configuration (debug, release, etc...).
The security cookies can be removed by playing with the buffer security check (/GS by default)