Compiling in Windows with GCC/MinGW

221 views
Skip to first unread message

Ed

unread,
Sep 7, 2018, 12:14:00 AM9/7/18
to benchmark-discuss
Hi,

This is a quick post as I need to catch some Zzzz's.  I'm using CLion with GCC 7.3.0 and MinGW 5.0.3 64 bit on Windows 10.  Other chains should be similar.  To get it compiled and running with minimal effort:


1) Create a new project named "GoogleBenchmark"

2) Extract and/or place "benchmark-master" and all its sub folders under your "GoogleBenchmark" project folder -- i.e "GoogleBenchmark/benchmark-master"

3) Edit your "GoogleBenchmark/CMakeLists.txt" to include

    include_directories(benchmark-master/include)
add_subdirectory(benchmark-master)
add_executable(GoogleBenchmark main.cpp benchmark-master/src/benchmark.cc benchmark-master/src/benchmark_register.cc benchmark-master/src/colorprint.cc benchmark-master/src/commandlineflags.cc benchmark-master/src/complexity.cc benchmark-master/src/console_reporter.cc benchmark-master/src/counter.cc benchmark-master/src/csv_reporter.cc benchmark-master/src/json_reporter.cc benchmark-master/src/reporter.cc benchmark-master/src/sleep.cc benchmark-master/src/statistics.cc benchmark-master/src/string_util.cc benchmark-master/src/sysinfo.cc benchmark-master/src/timers.cc)
target_link_libraries(GoogleBenchmark shlwapi.lib)


4) Edit your "GoogleBenchmark/main.cpp" to be as below -- no more, no less.

    #include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
BENCHMARK(BM_StringCreation);

static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();

5) Compile and run

---------------------------------------------------------
Running C:\Users\Ed\CLionProjects\GoogleBenchmark\cmake-build-release\GoogleBenchmark.exe
Run on (8 X 3606 MHz CPU s)
CPU Caches:
L1 Data 32K (x4)
L1 Instruction 32K (x4)
L2 Unified 262K (x4)
L3 Unified 8388K (x1)
Benchmark Time CPU Iterations
---------------------------------------------------------
BM_StringCreation 0 ns 0 ns 1000000000
BM_StringCopy 8 ns 8 ns 74666667

6) Yes, it can be done much better.. but this will get you going :)

Happy coding.

Ed

unread,
Sep 8, 2018, 8:32:10 PM9/8/18
to benchmark-discuss
Now Creating the actual library instead...

Install your toolchain and get it working with your IDE
    Visit http://mingw-w64.org/doku.php/download for details

Download benchmark-master.zip from https://github.com/google/benchmark

Extract benchmark-master.zip to your folder of projects
C:\Users\Ed\CLionProjects

Getting you
C:\Users\Ed\CLionProjects\benchmark-master

Stat Clion
Select Open


Navigate to C:\Users\Ed\CLionProjects\benchmark-master
Click OK


The IDE will load the environment and display a few errors as we did not include Google Test

Select Add Configuration… on the CLion toolbar
Click on + and create a configuration based on Application
Be sure to name it benchmark to match the downloaded CMakeLists.txt
Select All targets


Create a release configuration select File / Settings
Select Build, Execution, Deployment / CMake
The Debug configuration will be shown
Click on + and a Release configuration will be created
To disable Google Test as we are not download those libraries ensure the CMake options says
    -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_GTEST_TESTS=OFF
Click on the Debug Configuration and similarly update the CMake options says
    -DCMAKE_BUILD_TYPE=Debug -DBENCHMARK_ENABLE_GTEST_TESTS=OFF
Click OK


CMake will run and you should see all check pass now
After which the build button (hammer) will be enabled


Select Release as your configuration and build it


The library will be at
    C:\Users\Ed\CLionProjects\benchmark-master\cmake-build-release\src
And be named
    libbenchmark.a


You can use it in your Windows GCC projects even though it is called .a instead of .lib

Example of CMakeLists.txt for your project using the library (the order matters)
   
    cmake_minimum_required(VERSION 3.12)
    project(MyProj)
   
    set(CMAKE_CXX_STANDARD 17)
   
    link_directories(${PROJECT_SOURCE_DIR}/lib/benchmark)
   
    add_executable(MyProj main.cpp)
   
    target_link_libraries(MyProj libbenchmark.a shlwapi.lib)


soko...@gmail.com

unread,
Mar 7, 2020, 1:45:27 PM3/7/20
to benchmark-discuss
Hi, I wanted to ask if benchmark-master folder should be placed in the project one before or after building the library?
In either case, I still can't get rid of errors and having a really hard time managing this benchmark.

Thanks in advance!
Reply all
Reply to author
Forward
0 new messages