【问题标题】:How do I set C11 as the standard for compilation under CMake, clang and Visual studio?如何将 C11 设置为 CMake、clang 和 Visual Studio 下编译的标准?
【发布时间】:2020-02-09 01:39:52
【问题描述】:

有谁知道在使用 Visual Studio 2019、CMake 和 Clang 时如何将 C 标准设置为 C11。

目前,我有这个,但由于某种原因,clang 无法将其识别为标志:

if(WIN32) 
    add_definitions("-std=c11 -D _CRT_SECURE_NO_WARNINGS")
endif(WIN32)

而是将其编译为 C++

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.10.2)
PROJECT(cpu LANGUAGES C VERSION 0.0.1 DESCRIPTION "6502 emulator")

set(CMAKE_BINARY_DIR "./")
set(CMAKE_C_STANDARD 11)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_BUILD_TYPE DEBUG)
find_package(Curses COMPONENTS)

file(COPY "./headers/cpu.h" DESTINATION "${CMAKE_BINARY_DIR}/headers/cpu.h")

include_directories(headers)

file(GLOB CPU_SOURCES "src/cpu.c" "src/logger.c" "src/cartridge.c")
file(GLOB MONITOR_SOURCES "src/ui/[A-Za-z]*.c")


if(WIN32) 
    add_definitions("-std=c11 -D _CRT_SECURE_NO_WARNINGS")
endif(WIN32)

if(UNIX)
    set(CMAKE_C_FLAGS_DEBUG "-g -O0 -std:c11 -Wall -Wextra -fsanitize=address -fsanitize=undefined -DDEBUG")
    set(CMAKE_C_FLAGS "-std=c11")
    target_link_options(cpu PUBLIC "-D DEBUG")
    target_link_options(monitor PUBLIC "-D DEBUG ")
endif(UNIX)

add_library(cpu STATIC ${CPU_SOURCES})
add_executable(monitor ${MONITOR_SOURCES})



set_target_properties(cpu PROPERTIES PUBLIC_HEADER "../headers/cpu.h")

target_link_libraries(monitor cpu)

if(Curses_FOUND)
    include_directories(${CURSES_INCLUDE_DIR})
    target_link_libraries(monitor ${CURSES_LIBRARIES})
else (NOT Curses_FOUND)
    target_link_libraries(monitor)
endif(Curses_FOUND)

【问题讨论】:

  • 请发布您遇到的错误以及您如何设置项目。
  • 好吧,这不一定是一个错误,只是一个警告,但我不太喜欢它,我得到了 ISO C++11 does not allow conversion from string literal to 'char *''long long' is incompatible with c++98,但我通过使我添加到主帖中的 CMakeLists.txt
  • @visu: 也发布错误信息。
  • -std:c11?这是不对的。
  • 根据一些 SO 帖子,这也是错误消息

标签: c windows visual-studio cmake clang


【解决方案1】:

我刚刚发现clang显然使用官方的windows SDK编译,所以我很确定这就是我不能使用c11的原因。

来源:https://nullprogram.com/blog/2016/06/13/

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 2023-04-01
    • 2010-10-16
    • 2016-09-19
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多