I'm trying to include a precompiled header file into my project by adding -include myprecompiledheader.h to my compiler flags. However, when I use the -H option in GCC, this file doesn't show up in the list and thus I can't look at the "!" or "x" to determine if my .pch file is being used or not. Is there some way to tell if my precompiled header is being used when I -include it?
You can use a dummy header which contains only an #error directive, and put that in the same place as the .gch file. For example:
project/precompiled.h // your precompiled header
project/pre.h.gch // precompiled.h compiled into a gch
project/pre.h // contains only "#error "Not using the GCH."
Then, when you -include pre.h, if the compiler doesn't use the gch file it'll throw an error.
Related
I have a cmake project with an application and a library. The library has one header file generated with configure_file. The problem is that the application code cannot find the generated header file.
What is the proper way to include the generated header file path to the application -I options? target_link_libraries adds the path, but to the source but not to the binary directory?
Is it possible to add a property to the library when add_library is used so that this property can be used when target_link_libraries is used?
If you have generated your header file with
configure_file(config.h.in config.h #ONLY)
and have your library target already created with
add_library(libtarget ${SOURCE_FILES})
it's simply a call to
target_include_directories(libtarget PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
under the assumption that all commands are given in this order and are located in the same CMakeLists.txt file.
Background
I have a number of headers as part of a library (call it A) and are also used externally from other libraries (call it C).
I'd like to compile as part A's compilation to ensure the headers are self-contained. (Currently, this involves compiling C and if there are issues, re-compile A & make a new release).
Question
What's the best way to compile headers and discard the results? I am only interested in their successful compilation.
I am thinking of copy and rename them as cpp files (it's a C++ project) and then create a library out of them to check errors. But is there a simpler solution?
My aim is to the command-line equivalent of
g++ [compile_flags] -c header.hpp
and check for errors but not interested in the produced files.
I'd like something that works with cmake 3.13.5 (or older).
Thanks
Since I ran into the same question recently, let me show the solution I went with:
When you list the header files in your target's sources and only use relative paths below the current path (i.e. no .. in the paths), you can use a function like the following to compile a separate cpp file for each hpp file.
function(check_headers target)
# build object library used to "compile" the headers
add_library(${target}_headers OBJECT)
target_link_libraries(${target}_headers PRIVATE ${target})
target_include_directories(${target}_headers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# add a proxy source file for each header in the target source list
get_target_property(TARGET_SOURCES ${target} SOURCES)
foreach(TARGET_SOURCE ${TARGET_SOURCES})
if ("${TARGET_SOURCE}" MATCHES ".*\.hpp$")
set(HEADER_SOURCEFILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_SOURCE}.cpp")
file(WRITE "${HEADER_SOURCEFILE}" "#include \"${TARGET_SOURCE}\"")
target_sources(${target}_headers PRIVATE "${HEADER_SOURCEFILE}")
endif()
endforeach()
endfunction()
It adds an object library that compiles each header file in the given target.
Many of the open source projects include a set of directories with CMake's SYSTEM attribute. With this, these header files are provided to gcc with -isystem option:
include_directories(SYSTEM ${pybind11_INCLUDE_DIRS})
The inclusion of C++ headers using -isystem has issues on AIX as gcc on AIX by default adds implicit extern "C" for headers included with -isystem option. This causes templates in extern "C" errors on AIX. (More details can be found at here)
So, was trying to see if we can use any CMake feature to have -I option used for these C++ headers, though these are included with SYSTEM option on AIX. Otherwise, we need to change a whole lot of include_directories() invocations in these projects for AIX alone.
I came across CMAKE_NO_SYSTEM_FROM_IMPORTED attribute which sets the default value of NO_SYSTEM_FROM_IMPORTED for targets. But, setting it didn't override the SYSTEM attribute set in include_directories().
Please let me know whether we are using CMAKE_NO_SYSTEM_FROM_IMPORTED rightly and if there is any other CMake option to achieve the above.
I am having problems including frameworks in my project.
I have downloaded the private APIs from https://github.com/nst/iOS-Runtime-Headers/tree/master/PrivateFrameworks
This gives me header files to use with the framework already in xcode as I understand it.
I have added the header files to the framework folder in xcode for bluetoothmanager.
I have also included this framework in xcode.
The "framework search paths" is:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/PrivateFrameworks
Here is a picture of my xcode folder structure:
When I try to include the header file i get the error "BluetoothManager/BluetoothManager.h file not found".
Here is the code for that:
#include <BluetoothManager/BluetoothManager.h>
Objective-C has this in common with C/C++; the quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h"), while the angle-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include <math.h>).
So to have your own headers use < > not " " you need to pass either the relative or the absolute path for your header directory to the compiler.
I am writing an opencl kernel with many helper function in the same .cl file. I want to package the helper function into a header file. I read the spec and see I should pass -I Dir in the clCreateProgram function call.
I am still confused here. When I crate a A.h file to be included in the .cl file. Should I create a corresponding A.c file or A.cl file?
If I create a corresponding A.c file, some routines in .c file and .cl file might be different and might make the function cannot be used directly by the kernel.
You can have a header file and #include that in the kernel file. So to include file1.h in kernel.cl you need to add #include "file1.h" and as build option pass -I. if header file is in the same folder as kernel file or i.e. -I./include if in the include folder.