给定源文件,source.cpp我如何生成适当的标头source.hpp?
我在 linux 64 位下,我想避免手动编写标头,以减少我花在编写代码上的时间并限制可能的错误。
我目前正在尝试在 Windows Ubuntu 的 Linux 子系统中使用 cmake 进行针对 Windows 的交叉编译。我正在从 Linux 进行编译,因为我希望能够支持多个平台。
我不确定如何使其工作,并且对编译器没有偏好。
请参阅以下代码并构建我当前尝试的输出。
CMakeLists.txt file
cmake_minimum_required(VERSION 3.7)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/win.toolchain.cmake)
project(environment.exe)
find_package(SDL2 REQUIRED)
include_directories(environment.exe ${SDL2_INCLUDE_DIRS})
# get executable src files
file(GLOB_RECURSE environment.exe_src ${CMAKE_CURRENT_SOURCE_DIR}/src/environment/*.cpp)
# add environment.exe executable
add_executable(environment.exe ${environment.exe_src})
target_link_libraries(environment.exe ${SDL2_LIBRARIES})
# set output dir
set_target_properties(
environment.exe
PROPERTIES
# ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/environment"
)
Run Code Online (Sandbox Code Playgroud)
win.toolchain.cmake file
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x64)#i686
set(triple x64-pc-win32)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})
Run Code Online (Sandbox Code Playgroud)
BASH output
a@DESKTOP-DCHFQSJ:/mnt/e/d/User/Software Projects/windows-software-environment$ ./build.sh
Checking for …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include <any>
struct A {
A();
A(const A&) = default;
explicit A(std::any value);
};
struct B: A {
B() : A() { }
B(const B& b) : A(b) {}
explicit B(std::any value) : A(value) {}
};
Run Code Online (Sandbox Code Playgroud)
编译器在编译时报告以下错误(使用 clang++-5 或更高版本):
In file included from <source>:2:
In file included from /opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/any:37:
In file included from /opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/new:40:
In file included from /opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/exception:143:
In file included from /opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/nested_exception.h:40:
In file included from /opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/move.h:54:
/opt/compiler-explorer/gcc-7.3.0/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/type_traits:149:31: error: no member named 'value' in 'std::is_copy_constructible<B>'
: public …Run Code Online (Sandbox Code Playgroud) 我在 Windows 10 上开发包含 C++ 代码的 R 包。
我想检查内存分配错误。听起来最简单的路径是使用 clang++ 和选项-fsanitize=address。
如何配置 R 以使 clang 成为默认编译器?
到目前为止我所做的:
安装LLVM,将二进制文件添加到路径中
在 中设置以下选项~/.R/makevars:
CC = "path/to/llvm/bin/clang"
CXX = "path/to/llvm/bin/clang++"
CXX11 = "path/to/llvm/bin/clang++"
CXX14 = "path/to/llvm/bin/clang++"
CXXFLAGS = -O1 -g -fsanitize=address -fno-omit-frame-pointer
PKG_CXXFLAGS = -Og -fsanitize=address
LDFLAGS = -fsanitize=address
Run Code Online (Sandbox Code Playgroud)
devtools::install(). 我懂了:- installing *source* package 'TreeTools' ...
** using staged installation
** libs
"path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include' -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations …Run Code Online (Sandbox Code Playgroud) 当我使用 clang (10.0.1) 编译时:
#include <iostream>
template <typename ...Args>
void f( int a = 4, Args&&... aArgs )
{
std::cout << a << std::endl;
}
int main( int argc, char *argv[] )
{
f( 1, 2 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到:
main.cpp:4:30: error: missing default argument on parameter 'aArgs'
Run Code Online (Sandbox Code Playgroud)
但关于默认参数的标准说法:
在函数声明中,在带有默认实参的形参之后,所有后续形参必须具有在此或同一作用域的前一个声明中提供的默认实参 (C++11 起) ...除非形参是从形参扩展而来包或者是函数参数包。
这是一个叮当声错误吗?
提示:我在 gcc 上尝试了一下,效果很好
根据博客文章C++ 协程:了解对称传输, 对称传输允许您挂起一个协程并恢复另一个协程,而无需消耗任何额外的堆栈空间。这可以防止堆栈溢出,当协程包含循环和co_await可能在该循环体内同步完成的任务时,可能会发生堆栈溢出。
尽管以下代码示例使用对称传输,但它会因堆栈溢出而崩溃。请注意,下面的代码是重现堆栈溢出的最小示例:例如,如果我Type在头文件中包含类型析构函数的定义,则不会出现堆栈溢出。
// type.h
#pragma once
struct Type {
~Type();
};
Run Code Online (Sandbox Code Playgroud)
// type.cc
#include "type.h"
Type::~Type() {}
Run Code Online (Sandbox Code Playgroud)
// main.cc
#include <cstdint>
#include <exception>
#include <type_traits>
#include <utility>
#include "type.h"
#if __has_include(<coroutine>) // when using g++
#include <coroutine>
namespace coro {
using std::coroutine_handle;
using std::noop_coroutine;
using std::suspend_always;
} // namespace coro
#elif __has_include(<experimental/coroutine>) // when using clang++
#include <experimental/coroutine>
namespace coro {
using std::experimental::coroutine_handle;
using std::experimental::noop_coroutine;
using std::experimental::suspend_always;
} // namespace …Run Code Online (Sandbox Code Playgroud) 运行用 clang 编译的这段小代码片段时,我发现了一个奇怪的行为:
#include <iostream>
#include <exception>
#include <typeinfo>
struct Foo : public std::exception {
std::string myString;
Foo(const std::string& str) : myString(str) {}
Foo() : Foo(typeid(*this).name()) {}
};
int main()
{
Foo f;
std::cout << f.myString;
}
Run Code Online (Sandbox Code Playgroud)
typeid(*this).name()委托构造函数内部调用的指令返回nullptr导致分段错误的 a。在委托构造函数调用期间,std::exception基类尚未初始化,这似乎是此行为的原因。
我想知道这段代码是否由于某种原因格式不正确,或者这种行为是预期的。
我无法使用 g++ 重现此错误,其中代码运行良好。
仅当基类是 std::exception 时才会发生这种情况,在任何其他情况下,即使在 clang 上也能正常工作。
源代码:
\n#include <cstdio>\n\nstatic const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\n\nint main() {\n // create decoding table\n unsigned long invalid = 64;\n unsigned long decodeBase64Table[256] = {};\n for (unsigned long i = 0; i < 256; i++)\n decodeBase64Table[i] = invalid;\n for (unsigned long i = 0; i < 64; i++) {\n decodeBase64Table[(unsigned char)encodeBase64Table[i]] = i;\n }\n printf("decodeBase64Table[65] = %lu\\n", decodeBase64Table[65]);\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\nApple Clang 11.0.3(预期输出,即使使用 -O3):
\n/t/s/1673994196 \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf /usr/bin/clang++ --version\nApple clang version 11.0.3 (clang-1103.0.32.62)\nTarget: x86_64-apple-darwin21.6.0\nThread model: posix\nInstalledDir: /Library/Developer/CommandLineTools/usr/bin\n/t/s/1673994196 …Run Code Online (Sandbox Code Playgroud) 我有以下代码
#include <iostream>
void foo(const int* const &i_p) {
std::cout << &i_p << std::endl;
}
int main () {
int i = 10;
int* i_p = &i;
std::cout << &i_p << std::endl;
foo(i_p);
}
Run Code Online (Sandbox Code Playgroud)
x86-64 clang 9.0.0输出上是
Program returned: 0
0x7ffc43de63f8
0x7ffc43de6400
Run Code Online (Sandbox Code Playgroud)
x86-64 clang 10.0.0 而在编译器资源管理器链接上,输出变为
Program returned: 0
0x7ffc9da01ef0
0x7ffc9da01ef0
Run Code Online (Sandbox Code Playgroud)
这里进行了什么优化来提供相同的地址?我相信应该实现一个临时对象,因为我们无法将低级const指针绑定到低级non-const。
c++ const-correctness language-lawyer clang++ reference-binding