Visual Studio CMake 项目找不到 C++ 标准包含

问题描述 投票:0回答:1

这是一个不起作用的 CMakeLists.txt。 我将一个项目从直接使用 WinAPI 接口迁移到使用 SDL2。这是生成的 CMakeLists.txt。除此之外,出于本次讨论的目的,我使用一个极其简化的测试程序作为源,该程序直接且仅依赖于 SDL2。

cmake_minimum_required(VERSION 3.28)
project(my-project)
set (CMAKE_CXX_STANDARD 11)

set (TEST TestCase.cpp)

#set (CMAKE_PREFIX_PATH C:\\msys64\\ucrt64)
#find_package(SDL2 REQUIRED)
#unset(CMAKE_PREFIX_PATH)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
include_directories(. Library Tools Toolkit APClass Library/jpeg ${SDL2_INCLUDE_DIRS})
# include_directories(SYSTEM C:\\msys64\\mingw64\\include)

include(Library/CMakeLists.txt)
include(Tools/CMakeLists.txt) 
include(APClass/CMakeLists.txt)

add_executable(TestProgram ${TEST} ${DRAWBOX} ${TOOLS} ${APCLASS}) 

target_compile_features(TestProgram PUBLIC cxx_std_11)
target_compile_options(TestProgram PRIVATE )

target_link_libraries(TestProgram ${SDL2_LIBRARIES}  libSDL2_gfx.a              
                    SDL2_image SDL2_ttf
                    ) 

它不会编译。在构建 TestCase.cpp 时出现错误:

Error   C1083      Cannot open include file: 'unistd.h': No such file or directory

当我尝试使用 CMAKE_PREFIX_PATH 时,问题变得明显,但这可能没有影响。再多的缓存清除也无济于事。 同样报告了各种其他标准头文件,例如“strings.h”。

我尝试添加第 13 行:

include_directories(SYSTEM C:\\sys64\\mingw64\\include)

但事实证明,这会引入不同编译器的标头,只会造成可怕的混乱。

这是一个有效的 CMakeLists.txt,它是失败的 CMakeLists.txt 的前身。程序编译并运行。

cmake_minimum_required(VERSION 3.28)
project(my-project)
set (CMAKE_CXX_STANDARD 11)

set (TEST Test/CheckerCheckerBoardBoard.cpp)

include_directories(. Library Tools Toolkit APClass Library/jpeg)

include(Library/CMakeLists.txt)
include(Tools/CMakeLists.txt)
include(APClass/CMakeLists.txt)

message( STATUS DRAWBOX=${DRAWBOX})
 
add_library(Drawbox STATIC ${DRAWBOX} ${TOOLS} ${APCLASS}) 
add_executable(TestProgram WIN32 ${TEST} ${DRAWBOX} ${TOOLS} ${APCLASS}) 

target_compile_features(Drawbox PUBLIC cxx_std_11)
target_compile_options(Drawbox PRIVATE -showIncludes)

target_compile_features(TestProgram PUBLIC cxx_std_11)
target_compile_options(TestProgram PRIVATE 
        )

target_link_libraries(TestProgram winmm.lib)

我正在努力找出两个 CMake 脚本之间的差异来解释这个问题。

出现这样的问题去哪里查找呢?我想提供一个详细的日志文件,但我需要一些帮助才能知道如何创建它。 但这里有一个 CMake 干净生成的日志:

1> CMake generation started for configuration: 'x64-Debug'.
1> Command line: "C:\Windows\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe"  -G "Ninja"  -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\ken\Desktop\prog24\Drawboxish\install\x64-Debug" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/arm64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/arm64/cl.exe"   -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "C:\Users\ken\Desktop\prog24\Drawboxish" 2>&1"
1> Working directory: C:\Users\ken\Desktop\prog24\Drawboxish\build\x64-Debug
1> [CMake] -- Configuring done (0.2s)
1> [CMake] -- Generating done (0.0s)
1> [CMake] -- Build files have been written to: C:/Users/ken/Desktop/prog24/Drawboxish/build/x64-Debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted toolchain configurations.
1> Extracted includes paths.
1> CMake generation finished.

这是 TestCase.cpp。

#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_ttf.h>


SDL_Window* gWindow = NULL;             //The window we'll be rendering to
SDL_Renderer* gRenderer = NULL;         // Renders to an offscreen "texture" (frequent)
static SDL_Texture *target;
SDL_Event event;
TTF_Font *gFont;

void DrawNow();

int points[][2] = {
    {50, 50},
    {200, 100},
    {50, 300},
    {400, 400},
    {300, 70},
    {70, 450}
};


void DrawText(int x, int y, const char* text, SDL_Color fg) {
    int width, height;

    if (strlen(text) == 0) return;
    
    SDL_Color bgColor = { 0xff, 0xff, 0xff, 0xff};
    SDL_Surface* textSurface = TTF_RenderText_Blended(gFont, text, fg);
    if( textSurface == NULL )
        printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    else {
        //Create texture from surface pixels
        SDL_Texture *mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
        if( mTexture == NULL ) {
            printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
        } else {
            //Get image dimensions
            width = textSurface->w;
            height = textSurface->h;
        }
        //Get rid of old surface
        SDL_FreeSurface( textSurface );

        //Render to screen
        int descent = TTF_FontDescent(gFont);
        //printf("height: %d  descent:%d\n", height, descent);
        SDL_Rect renderQuad = { x, y-height-descent, width, height };
        SDL_RenderCopy( gRenderer, mTexture, NULL, &renderQuad);
        SDL_DestroyTexture(mTexture);
    }
}

void TextTest() {
    //Rectangle(30, 30, 140, 80, filled + blue);
    SDL_Color c1 = {0xff, 0xff, 0x00, 0x00};
    SDL_Color c2 = {0xff, 0x00, 0xff, 0x00};
    DrawText(50, 50, "hello", c1);
    DrawText(60, 55, "goodbye", c2);
    DrawNow();
}

void AutoDraw() {   // "User" graphics
    SDL_Rect rr = {10, 10, 120, 120};
    int outcome = SDL_RenderSetViewport(gRenderer, &rr);
    filledCircleColor(gRenderer, 100, 100, 40, 0xff00ff00);
    sleep(1);
    DrawNow();
}

void DrawNow() {
    while (SDL_PollEvent(&event)) {
    }

    SDL_SetRenderTarget(gRenderer, NULL);
    SDL_RenderCopy(gRenderer, target, NULL, NULL);
    SDL_RenderPresent(gRenderer);
    SDL_SetRenderTarget(gRenderer, target);
}

bool drawbox_inited = false;

int main() {
    if (! drawbox_inited) {
        //Initialize SDL
        if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
            printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
            return 1;
        }
        gWindow = SDL_CreateWindow("Drawbox", 0, 0, 1200, 900, 0);
        if( gWindow == NULL )    {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            return -1;
        }
        //Create renderer for window
        gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
        if( gRenderer == NULL )
        {
            printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
            return -1;
        }
        /* Create texture for display buffering */
        target = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 1200, 900);
        SDL_SetRenderTarget(gRenderer, target);
        //SDL_RenderSetLogicalSize(gRenderer, 2000, 1400);

        //Clear buffer
        SDL_SetRenderDrawColor( gRenderer, 0, 0, 0, 0xff );
        SDL_RenderClear( gRenderer );

        // Set up for fonts and text
        if (TTF_Init() < 0) {
            printf("ttf could not be initialized: %s\n", SDL_GetError());
        } else {
            gFont = TTF_OpenFont( "Cabin-Regular.ttf", 20 );
            if( gFont == NULL )
            {
                printf( "Failed to load default font! SDL_ttf Error: %s\n", TTF_GetError() );
            }
        }

        // handle window creation events: A MUST!
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            // printf("Event: %d\n", event.type);
        }
    }


    //ClearClip();

    srand(time(NULL));

    AutoDraw();
    sleep(6);

    return 0;
}
c++ visual-studio cmake std
1个回答
0
投票

'unistd.h' 是 POSIX 兼容系统的本机,我听说过几个等效项,但我认为这取决于您试图从中获取的功能,但其中大多数通常位于 ' windows.h'

如果 unistd.h 是必需的,而您的代码没有等效项,那么它唯一可以工作的情况是在 POSIX 环境中,这意味着任何基于 UNIX 的东西(macOS 也可以做到这一点,但请记住,并非所有情况)操作系统与 POSIX 完全兼容)

© www.soinside.com 2019 - 2024. All rights reserved.