简体   繁体   English

在Visual Studio 2013中编译简单的C代码

[英]Compile simple C code in Visual Studio 2013

I am trying to compile a very simple C code in VS2013. 我正在尝试在VS2013中编译非常简单的C代码。 I did a bit of Googling beforehand and I realised I need to follow certain steps for that eg change the compiler from Default to Compile As C Code in my project properties. 我事先做了一些谷歌搜索,我意识到我需要遵循一些步骤,例如,在项目属性中将编译器从“ Default更改为“ Compile As C Code

The problem is that when I compile following code: 问题是当我编译以下代码时:

#include <stdio.h>
main()
{
    printf("Hello World! \n");
    sleep(5);
}

I get these errors: 我得到这些错误:

Error   1   error LNK2019: unresolved external symbol _sleep referenced in function _main
Error   2   error LNK1120: 1 unresolved externals

And if I change the code to: 如果我将代码更改为:

#include <stdio.h>
#include <Windows.h>
main()
{
    printf("Hello World! \n");
    Sleep(5000);
}

Then it works fine. 然后工作正常。

How can I get my first code compiled successfully? 如何使我的第一个代码成功编译? I have some tested codes in C and I need to use them. 我在C中有一些经过测试的代码,需要使用它们。 If I couldn't resolve above issue, then I have to update all syntaxes. 如果无法解决以上问题,则必须更新所有语法。

How can I get my first code compiled successfully? 如何使我的第一个代码成功编译?

You can't (at least exactly with this code as you stated). 您不能(至少与您声明的这段代码完全一样)。 The point is that <stdio.h> header does not provide declaration for sleep() and C standard requires, that every function needs to be declared before its call. 关键是<stdio.h>头文件不提供sleep()声明,并且C标准要求,每个函数都需要在调用之前声明。 It's as simple as that. 就这么简单。

The second thing is that sleep() function, that you want is likely from POSIX (specifically from <unistd.h> header). 第二件事是所需的sleep()函数可能来自POSIX (特别是来自<unistd.h>头)。 However, Visual Studio 2013 is not POSIX-compliant. 但是,Visual Studio 2013 兼容POSIX。 Thus, the solution is to use Sleep() , that is declared in <Windows.h> header or possibly some other alternative. 因此,解决方案是使用<Windows.h>标头中声明的Sleep()或其他可能的替代方法。

Sleep is not a C standard function. 睡眠不是C标准功能。

If you are on a UNIX system, then include <unistd.h> . 如果您在UNIX系统上,请包括<unistd.h> However, if you are on a windows system, it should be in <windows.h> (which is why your second version of the code compiles successfully. 但是,如果您使用的是Windows系统,则该文件应位于<windows.h> (这就是第二版代码可以成功编译的原因。

If you want to use in both systems, try something like this (from here ): 如果要在两个系统中都使用,请尝试以下类似操作(从此处开始 ):

#ifdef __linux__ 
    //linux include code goes here
#elif _WIN32
    // windows include code goes here
#else

#endif

sleep is a non-standard function: sleep是一项非标准功能:

  • On UNIX use <unistd.h> 在UNIX上,使用<unistd.h>
  • On MS-Windows use <windows.h> 在MS-Windows上,使用<windows.h>

There is no way work around this besides writing your own code: 除了编写自己的代码,没有其他方法可以解决此问题:

void sleep (int seconds)
{
    Sleep (seconds * 1000);  // Converting to milliseconds
}

The sleep function is not declared in <stdio.h> . 睡眠功能未在<stdio.h>声明。 If you're on unix, use sleep() from unistd.h, or for windows, Sleep() from Winbase.h (windows.h). 如果您使用的是Unix,请使用unistd.h中的sleep() ,对于Windows,请使用Winbase.h(windows.h)中的Sleep() )。 If you want to see how Sleep() works, you can see how is sleep implemented at OS level? 如果您想了解Sleep()工作原理,那么可以看到在OS级别如何实现睡眠? .

If you want to do it without having to change sleep() to Sleep() , you could use a macro: 如果要执行此操作而不必将sleep()更改为Sleep() ,则可以使用宏:

#define sleep(s) Sleep((s) * 1000)

Macros are bad, so you may not want to do this in production code, but the advantage of the macro is that it will be guaranteed to be inlined, which will result in faster code. 宏是不好的,因此您可能不想在生产代码中执行此操作,但是宏的优点是可以确保将其内联,这将导致更快的代码。

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

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