简体   繁体   English

[cmake]如何使用cmake在Windows上包含和链接系统库

[英][cmake]how to include and link system libraries on windows using cmake

this's my cpp code: 这是我的cpp代码:

#pragma once

#include <cstdio>

int main(char** args, int size)
{
    printf("aaaaa\n");
    return 1;
}

this's my CMakeLists.txt 这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
set(PROJECT_ROOT_PATH "./")
add_executable(app ${PROJECT_ROOT_PATH}/app.c)

my steps running command is following: 我的步骤运行命令如下:

cmake -G "NMake Makefiles" ./
nmake

when I run nmake, there are many errors like this: 当我运行nmake时,会出现很多这样的错误:

D:\Program_Filesx86\Microsoft Visual Studio 14.0\VC\include\cstdio(36): error C2054: expected '(' to follow 'using' [E:\cmake-test\app.vcxproj]
D:\Program_Filesx86\Microsoft Visual Studio 14.0\VC\include\cstdio(36): error C2061: syntax error: identifier 'using' [E:\cmake-test\app.vcxproj]

if keep a empty function there: 如果在那里保留一个空函数:

#pragma once

int main(char** args, int size)
{
    return 1;
}

nmake would works righ and output an executable file: app.exe nmake可以正常运行并输出可执行文件: app.exe

is the problem that I didn't specify includes and libraries of windows SDK? Windows SDK的包含和库中没有指定的问题是什么? if so, how can I configurate them? 如果是这样,我该如何配置它们?

File cstdio might contain C++ specific stuff. 文件cstdio可能包含C ++特定的内容。 C++ is a superset of a subset of C, so not every C++ code is valid C. C ++是C子集的超集,因此并非每个C ++代码都是有效的C。

CMake compiles your .c file as C, thus syntax errors. CMake将您的.c文件编译为C,因此出现语法错误。

In your case the source of the error is C++ using statements that do not exist in C programming language. 在您的情况下,错误的根源是using C语言中不存在的语句的C ++。

To fix the problem either: 要解决此问题,请执行以下操作:

  • #include <stdio.h> (and compile as C as it was) #include <stdio.h> (并按原样编译为C)

or 要么

  • rename the file to .cpp (so it would be compiled as C++) 将文件重命名为.cpp (以便将其编译为C ++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM