Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can"t find PCH file during build. #24

Closed
ScottMcMichael opened this issue Sep 8, 2018 · 8 comments
Closed

Can"t find PCH file during build. #24

ScottMcMichael opened this issue Sep 8, 2018 · 8 comments

Comments

@ScottMcMichael
Copy link

I am testing this module out with our software but I can"t get it to compile. After calling add_library(), I call target_precompiled_header(${target} ${pch_path}) to add the PCH to it. CMake will run successfully if I use the absolute or relative path to the .pch file but when I try to compile the code I get one of the following errors:

(relative path)
cc1plus: fatal error: /home/smcmich1/repo/visionworkbench/build/src/vw/Core/CMakeFiles/VwCore.pch.dir/../stdafx.h: No such file or directory

(absolute path)
cc1plus: fatal error: /home/smcmich1/repo/visionworkbench/build/src/vw/Core/CMakeFiles/VwCore.pch.dir//home/smcmich1/repo/visionworkbench/src/vw/stdafx.h: No such file or directory

Do you know what could be going wrong? The PCH file is located at /home/smcmich1/repo/visionworkbench/src/vw/stdafx.h. CMake is being called from the "build" subfolder and the code is located in the "src" subfolder. The project line is:
project (VisionWorkbench C CXX CXXPCH CPCH)

This is on Red Hat 6, with CMake 3.5.2, and GCC 4.9.3.

Thanks,
Scott

@ericwa
Copy link

ericwa commented Sep 11, 2018

I think I"m getting the same thing with the example CMakeLists.txt from the readme. macOS 10.13, cmake version 3.12.2, ninja 1.8.2.

For me what happens is the first time I run ninja it fails, but it seems to build the pch at the end. Running ninja again works.

The makefile generator works for me; it builds the prefix.h target first and succeeds on the first try.

Erics-MacBook-Pro:build ericwa$ cmake .. -GNinja
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
n-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ericwa/pchtest/build
Erics-MacBook-Pro:build ericwa$ ninja -v
[1/6] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/engine.cpp.o -MF CMakeFiles/engine.dir/src/engine.cpp.o.d -o CMakeFiles/engine.dir/src/engine.cpp.o -c ../src/engine.cpp
FAILED: CMakeFiles/engine.dir/src/engine.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/engine.cpp.o -MF CMakeFiles/engine.dir/src/engine.cpp.o.d -o CMakeFiles/engine.dir/src/engine.cpp.o -c ../src/engine.cpp
<built-in>:1:10: fatal error: "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h" file not found
#include "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[2/6] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/library.cpp.o -MF CMakeFiles/engine.dir/src/library.cpp.o.d -o CMakeFiles/engine.dir/src/library.cpp.o -c ../src/library.cpp
FAILED: CMakeFiles/engine.dir/src/library.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/library.cpp.o -MF CMakeFiles/engine.dir/src/library.cpp.o.d -o CMakeFiles/engine.dir/src/library.cpp.o -c ../src/library.cpp
<built-in>:1:10: fatal error: "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h" file not found
#include "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[3/6] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/demo.dir/src/demo.cpp.o -MF CMakeFiles/demo.dir/src/demo.cpp.o.d -o CMakeFiles/demo.dir/src/demo.cpp.o -c ../src/demo.cpp
FAILED: CMakeFiles/demo.dir/src/demo.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/demo.dir/src/demo.cpp.o -MF CMakeFiles/demo.dir/src/demo.cpp.o.d -o CMakeFiles/demo.dir/src/demo.cpp.o -c ../src/demo.cpp
<built-in>:1:10: fatal error: "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h" file not found
#include "/Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[4/6] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -fPIC -x c++-header -o CMakeFiles/engine.pch.dir/src/prefix.h.pch -c ../src/prefix.h
ninja: build stopped: subcommand failed.

Running it again succeeds:

Erics-MacBook-Pro:build ericwa$ ninja -v
[1/5] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/library.cpp.o -MF CMakeFiles/engine.dir/src/library.cpp.o.d -o CMakeFiles/engine.dir/src/library.cpp.o -c ../src/library.cpp
[2/5] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Dengine_EXPORTS  -fPIC   -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/engine.dir/src/engine.cpp.o -MF CMakeFiles/engine.dir/src/engine.cpp.o.d -o CMakeFiles/engine.dir/src/engine.cpp.o -c ../src/engine.cpp
[3/5] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -include /Users/ericwa/pchtest/build/CMakeFiles/engine.pch.dir/src/prefix.h -MD -MT CMakeFiles/demo.dir/src/demo.cpp.o -MF CMakeFiles/demo.dir/src/demo.cpp.o.d -o CMakeFiles/demo.dir/src/demo.cpp.o -c ../src/demo.cpp
[4/5] : && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -dynamiclib -Wl,-headerpad_max_install_names  -o libengine.dylib -install_name @rpath/libengine.dylib CMakeFiles/engine.dir/src/engine.cpp.o CMakeFiles/engine.dir/src/library.cpp.o   && :
[5/5] : && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/demo.dir/src/demo.cpp.o  -o demo  -Wl,-rpath,/Users/ericwa/pchtest/build libengine.dylib && :
Erics-MacBook-Pro:build ericwa$

@gjasny
Copy link
Contributor

gjasny commented Sep 11, 2018

Last time I tried srcdir != builddir was not supported.

@ericwa
Copy link

ericwa commented Sep 11, 2018

Hm, I tried cmake . -GNinja && ninja in the srcdir (same directory as the CMakeLists.txt) and get the same behaviour - the PCH target isn"t built first as it should be, and the first ninja invocation fails.

@wildmichael
Copy link

I also stumbled across this issue. Problem is that the -include option that is added does not take into account the transformation applied by CMake to .. elements in the path name. Applying following patch fixes this particular problem for me with GNU compilers, not sure where else this issue is lurking though, hence not submitting it as a pull-request.

diff --git a/CMakePCHCompiler.cmake b/CMakePCHCompiler.cmake
index 525a836e..b04181db 100644
--- a/CMakePCHCompiler.cmake
+++ b/CMakePCHCompiler.cmake
@@ -118,6 +118,7 @@ function(target_precompiled_header) # target [...] header
 				)
 			target_sources(${target} PRIVATE $<TARGET_OBJECTS:${pch_target}>)
 		else()
+			string(REPLACE ".." "__" header "${header}")
 			set(exclude -include ${target_dir}/${header})
 		endif()
 		target_compile_options(${target} PRIVATE ${exclude})

@nanoant
Copy link
Owner

nanoant commented Apr 29, 2019

@themiwi Okay, so long story short - if I understand correctly - currently we cannot use PCH header path containing ...

Does CMake indeed translates .. to __? If yes I guess we ought do the same then, and PR would be much welcomed :)

@wildmichael
Copy link

Isn"t that what my patch is doing? 🤔

@nanoant
Copy link
Owner

nanoant commented Apr 30, 2019

@themiwi I was referring your statement:

Problem is that the -include option that is added does not take into account the transformation applied by CMake to .. elements in the path name.

And I was asking if it is really CMake behavior? But I found the evidence myself there in CMake"s cmLocalGenerator::CreateSafeUniqueObjectFileName.

And btw. it was NOT that trivial as you have suggested, as CMake does far more than string(REPLACE ".." "__" header "${header}") depending on the case.

@wildmichael
Copy link

Thanks, that"s as I suspected originally. With CMake there"s usually more to it 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants