Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't build Google Test with Visual Studio 2015 and Clang 3.7 with Microsoft CodeGen

Using VS 2015 and its new built-in clang toolset / project template, I cannot build Google Test successfully. I get the following errors:

Error       use of undeclared identifier 'chdir'; did you mean '_chdir'?    
Error       use of undeclared identifier 'fdopen'   
Error       use of undeclared identifier 'read' 
Error       use of undeclared identifier 'write'    
Error       use of undeclared identifier 'close'
Error       use of undeclared identifier 'O_RDONLY' 
Error       use of undeclared identifier 'O_APPEND' 
Error       use of undeclared identifier 'dup'; did you mean '_dup'?
Error       use of undeclared identifier 'creat'; did you mean '_creat'?

I noticed that the majority of those declarations are within these ANSI-checking blocks:

#if !__STDC__
...
#endif

Is there a project setting or something I can change to get these methods to resolve?

like image 260
WhittlesJr Avatar asked Jan 13 '16 21:01

WhittlesJr


People also ask

Can I use clang in Visual Studio?

Clang/LLVM support for both CMake and MSBuild projects is available in Visual Studio 2019 and Visual Studio 2022. You can use Visual Studio 2019 version 16.2 and later with Clang/LLVM to edit, build, and debug C++ Visual Studio projects (MSBuild) that target Windows or Linux.

How do I setup a Google test code in Visual Studio?

Add a Google Test project in Visual Studio 2022In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.

What is Google test adapter?

Google Test Adapter / Test Adapter for Google Test These test adapters are Visual Studio extensions that provide test discovery and execution of C++ tests written with the Google Test framework. Microsoft collaborated with the creators of GTA to fork their project and create 'Test Adapter for Google Test'.


1 Answers

I faced similar problems with chdir and freopen.

I will just post the steps I took, to get googletest up and running with VS2015 and Clang.

  • Get an LLVM snapshotbuild for windows. http://llvm.org/builds/
    (Make sure you download the correct version (32-/64-bit))

This will install a recent version of clang (at the time of writing v3.9). Be aware that this is a snapshot build and not an official release.

If you do not like snapshot builds, maybe try the latest release version. I did not test it. I just like to have up-to-date tools, especially when they are fast-paced like LLVM/Clang.

  • After the installation you should get entries in the Visual Studio Project properties. Properties -> General -> Platform Tools -> LLVM-vs2014 (and more) (Switch to LLVM-vs2014)

I am aware that you are asking for Clang 3.7 with Microsoft CodeGen. You have to decide on your own.
In addition, I do not like to apply some fixes/changes to code I did not write or do not know. As this worked out fine for me, I did not investigate the problem any further.

At this point it might already work for you. The next steps describe building googletest libraries and adding the include directories to the project.

  • Get googletest from github. https://github.com/google/googletest

  • Run cmake-gui and configure googletest to be able to build.

    Generator: Visual Studio 14 2015 Win64 (I only used 64bit, you can also try 32bit)

From the llvm documentation
(no link because not enough reputation: clang.llvm.org/docs/MSVCCompatibility.html):

First, Clang attempts to be ABI-compatible, meaning that Clang-compiled code should be able to link against MSVC-compiled code successfully.

  • Use default native compilers

Where is the source code: (ex. C:\libs\googletest\googletest)
(Because there is also googlemock in the top directory)

Where to build the binaries: (ex. C:\libs\googletest\build)

  • Uncheck: BUILD_SHARED_LIBS (build shared libs if you want) CMAKE_CONFIGURATION_TYPES: Debug and Release (choose others if you like)
    Remember or change: CMAKE_INSTALL_PREFIX (ex. C:\libs\googletest\install)

Python 2.7 was found by cmake, even though im pretty sure it is not necessary.
Press Configure and Generate.

  • After generating the solution file, go to the directory specified above (Where to build the binaries, ex. C:\libs\googletest\build) and open the solution gtest.sln.

  • Choose Debug solution configuration and right click ALL_BUILD and Build. When done, right click INSTALL and Build. This creates the folders specified earlier.

  • CMAKE_INSTALL_PREFIX (ex. C:\libs\googletest\install) in there you may want to change the libs name and add a *d.lib to keep the files from overwriting and as designator that it was the debug build.

  • Repeat the steps for the Release solution configuration. In CMAKE_INSTALL_PREFIX (ex. C:\libs\googletest\install) you should find an include directory and a lib directory.

  • In your project under Properties -> VC++ Directories add Include Directories. CMAKE_INSTALL_PREFIX<b>\include</b> (ex. C:\libs\googletest\install<b>\include</b>)

  • In your project under Properties -> VC++ Directories add Library Directories. CMAKE_INSTALL_PREFIX\lib (ex. C:\libs\googletest\install\lib)

  • And under Properties -> Linker -> Input -> Additional Dependencies (gtest.lib / gtestd.lib depending on your configuration)

After that I was able to build and run my tests.

like image 119
panzerfaust Avatar answered Oct 02 '22 00:10

panzerfaust