Unable to create Google Test with Visual Studio 2015 and Clang 3.7 using Microsoft CodeGen

Using VS 2015 and its new built-in clang toolbox / template set, I cannot successfully run Google Test. 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 most of these declarations fall within these ANSI validation blocks:

 #if !__STDC__ ... #endif 

Is there a project parameter or something that I can change to solve these methods?

+5
source share
2 answers

I am having similar problems with chdir and freopen.

I will just post the steps I took to run googletest and run with VS2015 and Clang.

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

This will install the latest version of clang (at the time of writing v3.9). Keep in mind that this is a snapshot assembly, not an official version.

If you don't like snapshot builds, maybe try the latest version. I did not check. I just like modern tools, especially when they change quickly, like LLVM / Clang.

  • After installation, you should get entries in the properties of the Visual Studio project. Properties โ†’ General โ†’ Platform Tools โ†’ LLVM-vs2014 (and more) (Switch to LLVM-vs2014 )

I know you are asking for Clang 3.7 with Microsoft CodeGen. You must decide for yourself.
In addition, I do not like to apply some corrections / changes to code that I did not write or do not know. Since this worked for me, I no longer explored the issue.

Now it can work for you. The following steps describe how to create googletest libraries and add include directories to your project.

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

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

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

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

First, Clang is trying to be ABI compatible, which means that Clang compilation code must be able to successfully compile code compiled using MSVC.

  • Use custom compilers by default

Where is the source code: (e.g. C:\libs\googletest\googletest )
(Because the top directory also has googlemock)

Where to build binaries: (e.g. C:\libs\googletest\build )

  • Uncheck the box: BUILD_SHARED_LIBS (create shared libraries if you want) CMAKE_CONFIGURATION_TYPES : Debug and release (select others if you want)
    Remember or change: CMAKE_INSTALL_PREFIX (e.g. C:\libs\googletest\install )

Python 2.7 was found by cmake, although I'm sure it is not necessary.
Click Customize and Generate.

  • After creating the solution file, go to the directory above (Where to build binary files, for example C:\libs\googletest\build ), and open the gtest.sln solution.

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

  • CMAKE_INSTALL_PREFIX (for example, C:\libs\googletest\install ) there you may want to change the name libs and add * d.lib so that the files are overwritten, and the debug assembly is used as a pointer.

  • Repeat the steps to configure the Release solution. In CMAKE_INSTALL_PREFIX (e.g. C:\libs\googletest\install ) you should find the include directory and the lib directory.

  • In your project, under Properties โ†’ VC ++ Directories, add Include directories. CMAKE_INSTALL_PREFIX<b>\include</b> (e.g. C:\libs\googletest\install<b>\include</b> )

  • In your project, under Properties โ†’ VC ++ Directories, add library directories. CMAKE_INSTALL_PREFIX \ lib (e.g. C: \ libs \ googletest \ install \ lib )

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

After that, I managed to create and run my tests.

+1
source

Due to debugging issues with the new LLVM / Clang, I spent some time with VS2015, Clang 3.7 and googletest.

I compiled googletest libraries as mentioned in another answer. Then he switched to "Clang 3.7 using Microsoft CodeGen (v140_clang_3_7)" to create my googletest project.

Error output example:
.... gtest / internal / gtest-port.h (2384.35): error: use of undeclared identifier "close"
inline int Close (int fd) {return close (fd); }

In the Microsoft documentation you can find:

Fdopen example
fdopen: This POSIX function is deprecated. Use ISO-compatible _fdopen instead. https://msdn.microsoft.com/en-us/library/ms235351.aspx

This is the same with some more features.

Just google for "visual studio xxx" where xxx is fdopen or chdir. You should get a link to the documentation where you can find a new method. (Usually the underscore before the name.)

Once you know what to use, go to the error (use the VS-output to go there), here is gtest-port.h (2384.35).

Make changes here
... return close (fd);
for the image ... return _ close (fd);
etc.

After that, my tests worked as usual. It was necessary to get rid of some warnings.

+1
source

Source: https://habr.com/ru/post/1240579/


All Articles