我尝试使用三个编译器(msvc2017,gcc8.2,clang7.0)的下一个代码,msvc2017一直工作,但gcc和clang没有.我想了解我的代码有什么问题,以及为什么编译器无法编译它.
#include <cassert>
#include <iostream>
#include <cstdlib>
class Downloader
{
public:
struct Hints
{
int32_t numOfMaxEasyHandles = 8;
//Hints(){} // <= if I uncomment this all works gcc+clang+msvc
//Hints() = default; // <= if I uncomment this neither clang no gcc works (msvc - works)
};
static Downloader *Create(const Hints &hints = Hints());
};
Downloader* Downloader::Create(const Hints &hints)
{
std::cout << hints.numOfMaxEasyHandles << std::endl;
return nullptr;
}
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以在https://wandbox.org/上自行使用此代码,并查看错误:
prog.cc:16:58: error: …Run Code Online (Sandbox Code Playgroud) 假设我们想在 cmake 多平台项目(ios、macos、android、windows、linux)中使用boost::file_system库。一种方法是直接将 boost 源代码复制到我们的项目中。它增加了项目大小并增加了很多维护问题、修补、更新等。如果我们在 cmake配置步骤中下载 boost 源会怎样?所以我添加了最小的示例(文件 - main.cxx):
#include <boost/filesystem.hpp>
#include <iostream>
int main(int, char**)
{
std::cout << boost::filesystem::current_path() << std::endl;
return std::cout.fail();
}
Run Code Online (Sandbox Code Playgroud)
接下来是完整的 CMakeLists.txt 文件,用于从源代码构建这个最小的示例,而无需将 boost 安装到系统中。
cmake_minimum_required(VERSION 3.20...3.23)
project(19-boost-file-system CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_ENABLE_EXPORTS OFF)
include(FetchContent)
fetchcontent_declare(
BoostAssert GIT_REPOSITORY https://github.com/boostorg/assert.git
GIT_TAG boost-1.79.0)
fetchcontent_declare(
BoostConfig GIT_REPOSITORY https://github.com/boostorg/config.git
GIT_TAG boost-1.79.0)
fetchcontent_declare(
BoostContainerHash
GIT_REPOSITORY https://github.com/boostorg/container_hash.git
GIT_TAG boost-1.79.0)
fetchcontent_declare(
BoostCore GIT_REPOSITORY https://github.com/boostorg/core.git
GIT_TAG boost-1.79.0)
fetchcontent_declare(
BoostDetail GIT_REPOSITORY https://github.com/boostorg/detail.git …Run Code Online (Sandbox Code Playgroud) 我尝试:
env = Environment(ENV = {'PATH' : os.environ['PATH'], \
'INCLUDE' : 'c:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\include\\', \
'LIB' : 'c:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\lib\\'})
Run Code Online (Sandbox Code Playgroud)
但它不起作用.错误信息:"cl"不是命令...
我在scons wiki中找到了描述(http://www.scons.org/wiki/PlatformSpecificNotes#Visual_C.2B-.2B-):如果使用Microsoft Visual C++,则需要设置'INCLUDE','LIB'和'PATH '在您的环境中,然后在创建'环境'对象时导入它们.这些将用于定位MSVC++工具并设置'CPPFLAGS'等.
我究竟做错了什么?
完整的sconstruct(它适用于ubuntu,以及带有mingw的windows):
import os
import sys
if ARGUMENTS.get('ndk', 0):
current_dir = os.getcwd()#os.path.dirname(os.path.abspath(__file__))
os.environ["NDK_PROJECT_PATH"] = current_dir + '/android-project'
os.system("ndk-build") # use V=1 if can't compile for android
exit(0)
if ARGUMENTS.get('ant', 0):
current_dir = os.getcwd()#os.path.dirname(os.path.abspath(__file__))
android_prj_path = current_dir + '/android-project'
os.chdir(android_prj_path)
os.system("ant debug") # use V=1 if can't …Run Code Online (Sandbox Code Playgroud) 我尝试在 Windows 10 上使用 KDevelop 5.2 并尝试在 CTRL+S 上自动格式化代码现在我添加了自定义源格式化程序(clang-format)并且它在单击时起作用 - 编辑 - > 重新格式化源,但是如何格式化文件上的代码保存操作?
我有最小的你好世界样本:
#include <cstdlib>
#include <iostream>
#include <string_view>
int main(int /*argc*/, char* /*argv*/ [])
{
using namespace std;
string_view output_phrase("hello world");
cout << output_phrase << endl;
bool is_good = cout.good();
int result = is_good ? EXIT_SUCCESS : EXIT_FAILURE;
return result;
}
Run Code Online (Sandbox Code Playgroud)
所以我创建了最小的CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.14)
project(01-1-hello-world CXX)
add_executable(01-1-hello-world main.cxx)
target_compile_features(01-1-hello-world PUBLIC cxx_std_17)
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用已知的 CMake 编译器,一切都会按预期工作(如 MSVC、clang++、g++)。但是如果我尝试使用一些 SDK 中的自定义编译器(基于 clang)和工具链文件cmake 说:
CMakeLists.txt:5 (target_compile_features) 处的 CMake 错误:target_compile_features CXX 编译器不知道编译器功能“cxx_std_17”
“铛”
CMAKE_CXX_COMPILE_FEATURES所以我尝试在我的工具链文件中设置
set(CMAKE_CXX_COMPILE_FEATURES cxx_std_17) # we know …Run Code Online (Sandbox Code Playgroud) 为什么寻找文件的末尾可以有用?为什么POSIX让我们在打开的文件中寻找只读的例子?
c ++:http://en.cppreference.com/w/c/io/fseek posix:https://www.unix.com/man-page/posix/3P/fseek/
下一代码我在MinGW-64w上测试
#include <cassert>
#include <cstdio>
#include <cstring>
int main() {
std::FILE* f = std::fopen("tmp_file.txt", "wb");
auto result = std::fwrite("1", 1, 1, f);
assert(result == 1);
result = std::fclose(f);
assert(result == 0);
f = std::fopen("tmp_file.txt", "rb"); // READ ONLY binary mode
result = std::fseek(f, 100500, SEEK_SET);
assert(result == 0); // WHY I can seek to not existing position in file?
// opended in READ_ONLY mode?
char buff[100500] = {0};
result = std::fread(&buff, …Run Code Online (Sandbox Code Playgroud)