google / benchmark

A microbenchmark support library
Apache License 2.0
8.61k stars 1.57k forks source link

[BUG] MSVC 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' #1655

Open lishaojiang opened 10 months ago

lishaojiang commented 10 months ago

Describe the bug A clear and concise description of what the bug is.

I have download and compile and test Google BenchMark success on My Linux, My Mac. but On My Windows PC , I get an error when other project link the benchmark!

I know this is because the benchmark project is a lib generated by MD, but the test project regards it as MT, so the link is wrong.

I am forced to manually modify the benchmark attribute to MT then build and then manually copy it to the installation directory . it can work well.

I have tried the code below adding to CMakeLists.txt, it don't work!

if (MSVC)
     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT")
endif

or adding MT to the build script, but it has no effect! cmake -E chdir "build" cmake -DCMAKE_CXX_FLAGS_RELEASE=/MT -DCMAKE_CXX_FLAGS=/MT -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../

System Which OS, compiler, and compiler version are you using:

To reproduce Steps to reproduce the behavior: install.bat

git clone https://github.com/google/benchmark.git
cd benchmark
cmake -E make_directory "build"
cmake -E chdir "build" cmake -DCMAKE_CXX_FLAGS_RELEASE=/MT -DCMAKE_CXX_FLAGS=/MT -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
cmake --build "build" --config Release --target install

then other project use the google bench

CMakeLists.txt

find_package(benchmark REQUIRED)
target_link_libraries(main PUBLIC benchmark::benchmark)
target_link_libraries(main PUBLIC benchmark::benchmark_main) 

Expected behavior A clear and concise description of what you expected to happen. How Can fix the google benchmark MD to MT auto

Screenshots If applicable, add screenshots to help explain your problem. image

Additional context Add any other context about the problem here.

lishaojiang commented 10 months ago

well done , I find one solution! Just use bleow code in Google BenchMark every CMakeLists.txt file! I find 4 cmakelists.txt file in my pc!

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    add_compile_options(/MT)
endif()

The original method didn't work, anyone know why

if (MSVC)
     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT")
endif