benchmark icon indicating copy to clipboard operation
benchmark copied to clipboard

Settings for visual studio

Open etheaven opened this issue 10 years ago • 14 comments

Hello, may i know how am i supposed to compile it with visual studio? throws unresolved external symbols, i believe your code is not wrong, it's probably because of the compiler.

etheaven avatar May 03 '16 19:05 etheaven

i'm not sure what you've tried, but this is a good starting point: https://cognitivewaves.wordpress.com/cmake-and-visual-studio/

once you have a solution file, you can build as normal in VS.

what are the symbols? it might be our code :)

dmah42 avatar May 03 '16 19:05 dmah42

If you have unresolved symbols you probably need to add shlwapi.lib to your linker inputs.

BillyONeal avatar May 10 '16 23:05 BillyONeal

@BillyONeal Should the CMake be linking shlwapi.lib instead?

EricWF avatar Aug 30 '16 06:08 EricWF

@EricWF: Don't know if it is possible to ask cmake to do that. See here:

https://github.com/google/benchmark/blob/6e259170011cd2fbac069b9d4ff83b42445a96cc/src/CMakeLists.txt#L36

The dependency on shlwapi.lib gets injected through a .obj that is packaged inside benchmark.lib (compiled from here), so it needs to be present on the linker command line for anyone who links against benchmark.lib.

BillyONeal avatar Aug 30 '16 08:08 BillyONeal

(MSVC++ has #pragma comment(lib, "shlwapi.lib"), but I don't believe MinGW supports anything like that; so if folks are using CMake they're better off manually specifying it in their CMakeLists.txt)

BillyONeal avatar Aug 30 '16 08:08 BillyONeal

Thanks Billy. I don't work on Windows so I'm trying to figure out if this is our bug, or just a general quirk of Windows compilation (like -pthread or -lm on Unix). If we ever get a CMake configuration module we should ensure shlwapi.lib is listed as an interface link library.

I think the best course of action is to prominently document the dependency as a known issue and close this bug.

EricWF avatar Aug 30 '16 08:08 EricWF

@BillyONeal Patches correctly implementing #pragma comment(lib, "shlwapi.lib") are welcome. I would do it myself but I'm unfamiliar with Windows.

EricWF avatar Aug 30 '16 09:08 EricWF

@EricWF: You can just stick that pragma in sysinfo.cc, and that lib will be added as a dependency for anyone who links against the resulting .obj. Putting it after #include <Shlwapi.h> should work: https://github.com/google/benchmark/blob/95dee3c699e0c972fca9ad414f80e451e0bf4d7f/src/sysinfo.cc#L19 -- but as I said I don't think that will work for MinGW targets.

BillyONeal avatar Aug 30 '16 17:08 BillyONeal

@BillyONeal , can you explain to Nooglers who aren't used to using Cmake how I can manually link in Shlwapi into my projects CMakeLists?

Edit: figured it out. Just added "shlwapi.lib" to target_link_library command in my project. (Do add an example line of code, as I went on a wild goose chase to find the physical location of that file not knowing minGW had it on its regular path)

winstondu avatar Nov 25 '17 07:11 winstondu

@winstondu In the same target_link_libraries where you add google test, just add shlwapi. I've been doing something like this:

function(add_benchmark BENCHNAME)
  add_executable("${BENCHNAME}" "${PROJECT_SOURCE_DIR}/benchmarks/${BENCHNAME}.cpp")
  target_link_libraries("${BENCHNAME}" benchmark)
  if (MSVC)
    target_link_libraries("${BENCHNAME}" Shlwapi.lib)
  endif()
  add_test("run_${BENCHNAME}" "${BENCHNAME}" "${BENCHMARK_OUTPUT_FORMAT}")
endfunction(add_benchmark)

BillyONeal avatar Nov 26 '17 22:11 BillyONeal

Once i build the INSTALL.vcproj and RUN_TESTS , how do i link the library to a new VS project

spapiernik avatar Oct 10 '19 18:10 spapiernik

@Cxx-mlr I have the same requirement to link the library to a new VS project. Did you get it working yet? If yes, can you please share the steps to do it?

ura1286 avatar Oct 15 '19 03:10 ura1286

If the benchmark project is generated with -DBUILD_SHARED_LIBS=on, it will work in Visual Studio - as long as the Additional Include Directories contain the benchmark/include folder and the Additional Library Directories contain the benchmark/lib folder.

There is no need to specify shlwapi.lib as additional dependency in the consuming project.

I don't think that the version being installed through vcpkg is build with shared libs on - I had to build it specifically off a forked version.

bstordrup avatar Jan 03 '24 15:01 bstordrup

is there a way to change the vcpkg installer version to include shared libs?

dmah42 avatar Jan 04 '24 09:01 dmah42