WhittlesJr
WhittlesJr

Reputation: 122

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?

Upvotes: 7

Views: 2262

Answers (2)

panzerfaust
panzerfaust

Reputation: 54

Due to troubles debugging with newer LLVM/Clang, i spent some time with VS2015, Clang 3.7 and googletest.

I compiled googletest libraries as mentioned in other answer. Then switched to "Clang 3.7 with Microsoft CodeGen (v140_clang_3_7)" to build my googletest project.

Sample output of error:
....gtest/internal/gtest-port.h(2384,35): error : use of undeclared identifier 'close'
inline int Close(int fd) { return close(fd); }

In the documentation of Microsoft you can find:

Example with fdopen
fdopen: This POSIX function is deprecated. Use the ISO C++ conformant _fdopen instead.
https://msdn.microsoft.com/en-us/library/ms235351.aspx

This is the same with quite some more functions.

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

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

Make the change, here
...return close(fd);
to
...return _close(fd);
and so on.

After that my tests worked as usual. Had to get rid of some warnings too.

Upvotes: 1

panzerfaust
panzerfaust

Reputation: 54

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.

Upvotes: 1

Related Questions