我试图在 cmake 中生成一个包含我经常使用的标准库的预编译头文件。当我运行 cmake 时没有错误,但是当我构建它时,它说它在 cmake_pch.h 中找不到头文件。
这是我的 cmake 脚本的片段,它添加了预编译头:
target_precompile_headers(fae-core PRIVATE
<algorithm>
<cstddef>
<fstream>
<string>
<sstream>
<memory>
<chrono>
)
Run Code Online (Sandbox Code Playgroud)
以下是运行 cmake 生成的 makefile 的完整输出:
Scanning dependencies of target fae-core
[ 6%] Building CXX object core/CMakeFiles/fae-core.dir/cmake_pch.hxx.gch
[ 12%] Building C object core/CMakeFiles/fae-core.dir/cmake_pch.h.gch
In file included from <command-line>:32:
/home/finn/dev/fae/build/core/CMakeFiles/fae-core.dir/cmake_pch.h:4:10: fatal error: algorithm: No such file or directory
4 | #include <algorithm>
| ^~~~~~~~~~~
compilation terminated.
make[2]: *** [core/CMakeFiles/fae-core.dir/build.make:78: core/CMakeFiles/fae-core.dir/cmake_pch.h.gch] Error 1
make[1]: *** [CMakeFiles/Makefile2:136: core/CMakeFiles/fae-core.dir/all] Error 2
make: *** [Makefile:84: all] …Run Code Online (Sandbox Code Playgroud) #pragma onceCodegear RAD Studio 2009 有什么相同的东西吗?
我正在使用预编译头向导,我想知道在包含头文件时是否仍然需要使用包含保护?
我需要一些关于如何设置我的项目的建议.我正在构建一个静态库,并想知道在我走得太远之前我使用预编译头的方式是否正确.
到目前为止,我的stdafx文件只包括(对于类似DWORD等类型)和(对于std :: string).
我构建了一个名为TestFuncs.cpp/h的简单cpp/header组合
TestFuncs.cpp:
#include "stdafx.h"
using namespace std;
void MyFunc(DWORD a, string b) {
// irrelevant
}
Run Code Online (Sandbox Code Playgroud)
TestFuncs.h
#pragma once
void MyFunc(DWORD a, std::string b);
Run Code Online (Sandbox Code Playgroud)
这里的代码编译正确.我遇到的问题是当我想在另一个项目中使用这个静态库时,我通常会做#include"path_to_my_static_lib_project/TestFuncs.h"
但是,这个问题基于TestFuncs.h,当时DWORD和string都是未知的,因为它们是从stdafx.h文件定义的类型.
我提出的一个解决方案(我不知道这样做是正确的)只是在#pragma一次之后将stdafx.h包含在TestFuncs.h的顶部.现在,项目使用预编译头文件工作文件.
这是应该怎么做,还是有正确的方法这样做?
谢谢.
我在几个预编译头文件和地址空间布局随机化不兼容的网站上阅读它.是什么让他们不相容?修复它需要什么?
我的问题是操作系统独立我听说这是linux上的grsecurity和更新的Windows版本的问题.(而且我不知道ASLR还有什么.)
我在我的项目中使用预编译的头文件.precompile.h包括我创建的*.h文件和来自我从未重建的库中的许多*.h文件.
我的问题是,每次编辑我的一个*.h文件时,它都会编译整个precompile.h文件.这大约需要5分钟.(仅编辑cpp文件后编译只需1秒钟.)
我知道这是预编译头文件的工作方式,但无论如何要分离我的头文件,以便编译器只编译我的文件?
如果即使在空的.cpp中也不包含stdafx,则会出现此错误
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
Run Code Online (Sandbox Code Playgroud)
为什么即使在虚拟文件中也需要包含它?
新的使用C
像stdlib这样的库的头文件不包含它们提供访问的函数的实际实现代码.据我所知,编译这样的库的实际源文本不需要编译,但具体如何? 这些库的实现细节是否包含在编译器中?
当你使用像printf()这样的函数时,包括头文件基本上粘贴在函数声明的代码中,但通常也需要实现代码. 它存储的形式是什么?(以及在哪里?)这个编译器是否具体?是否可以编写自定义代码并以这种方式引用它而无需修改编译器的行为?
我一直在寻找并找到一些相关但没有具体的信息.这可能与没有很好地制定问题有关.谢谢.
我所有的问题都与 vc++ 编译器有关,但我猜其他 c++ 编译器具有相同的行为。
//stdafx.h
#include <boost/variant/variant.hpp>
//test1.cpp
#include "stdafx.h"
#include <boost/variant/variant.hpp>
...
//test2.cpp
#include "stdafx.h"
...
Run Code Online (Sandbox Code Playgroud)
我有一个C ++控制台项目,在安装程序中包含一个预编译的标头。我不小心从项目中删除了该文件,现在因为我得到了而无法编译error C1083: Cannot open precompiled header file: 'Debug\Project.pch': No such file or directory。我是否需要重新创建项目并将所有代码导入其中?我尝试重新创建一个仅包含的名为“ stdafx.cpp”的文件#include "stdafx.h",但该文件不起作用。我该如何解决?
我有一个如下所述的反问题: 结合C++和C - #ifdef __cplusplus如何工作?
整个应用程序是C代码,现在我需要在那里添加一些C++函数.但在这样做时我得到了这个错误:
/tmp/cczmWtaT.o: In function `aocl_utils::_checkError(int, char const*, int, char const*, ...)':
/home/harp/host/../common/src/AOCLUtils/opencl.cpp:245: undefined reference to `cleanup()'
/tmp/ccrmKQaT.o: In function `main':
/home/harp/host/src/main.c:165: undefined reference to `harp_setup()'
/tmp/ccGKataf.o: In function `solver_propagate(solver_t*)':
/home/harp/host/src/solver.c:751: undefined reference to `launch_kernel()'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我试过了:
#ifdef __cplusplus
extern "C" {
#endif
<C code>
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
但它显示了同样的错误.
我正在做的是在C文件中包含一个C++头文件,其中包含我需要的外部函数.
例:
solver.c
#include "HarpBuffers.h"
...
Run Code Online (Sandbox Code Playgroud)
HarpBuffers.h
extern void harp_setup();
extern void setup_buffers(cl_int a, cl_int b, int **h_clause, unsigned int …Run Code Online (Sandbox Code Playgroud) 默认情况下,gcc 生成 .gch 预编译头文件。然而,clang 需要 .pch 头文件。在其他基于 gcc 的项目上使用 clang 运行 scan-build 时,我因此收到错误。可能导致此问题的两种文件格式之间有什么区别?