7

I am working on a UDP socket program in windows visual studio. I am getting an error for sprintf statement. How to correct it? My code is:

    for (i = 0; i < 30;i++) //take-off
{
    printf("send AT*REF:take off\n");
            sprintf(command, "AT*REF=%d,290718208\r", seq++);



    rc = sendto(sd, command, strlen(command) + 1, flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
    if (rc<0) {
        printf("%s: can not send data\n", argv[0]);
            return(1);
                    }
     }

The errors I am getting are :

Error   1 error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
Error   1 error LNK2019: unresolved external symbol __imp__bind@12
Error   2 error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _main 
Error   3 error LNK2019: unresolved external symbol __imp__htons@4
Error   4 error LNK2019: unresolved external symbol __imp__sendto@24
Error   5 error LNK2019: unresolved external symbol __imp__socket@12
Error   6 error LNK2019: unresolved external symbol __imp__gethostbyname@4
Error   7 error LNK1120: 6 unresolved externals 
abelenky
  • 63,815
  • 23
  • 109
  • 159
wireless_lab
  • 177
  • 1
  • 4
  • 18
  • 4
    Have you tried doing what the compiler tells you to? – user657267 Apr 09 '14 at 06:50
  • ya i tried it. it is giving a lot of errors. Error 1 error LNK2019: unresolved external symbol __imp__bind@12 Error 2 error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _main Error 3 error LNK2019: unresolved external symbol __imp__htons@4 Error 4 error LNK2019: unresolved external symbol __imp__sendto@24 Error 5 error LNK2019: unresolved external symbol __imp__socket@12 Error 6 error LNK2019: unresolved external symbol __imp__gethostbyname@4 Error 7 error LNK1120: 6 unresolved externals – wireless_lab Apr 09 '14 at 06:56
  • An unrelated hint: When a function returns with an error (like when `sendto` return with `rc < 0`) then it might be a good idea to tell the user *what* the error was. It will certainly help you to understand what went wrong. Read about [`WSAGetLastError`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms741580%28v=vs.85%29.aspx). – Some programmer dude Apr 09 '14 at 06:59
  • 1
    The compiler error, and your linker errors are unrelated. Actually do try using sprintf_s instead. – Dan Apr 09 '14 at 07:08
  • Another unrelated thing: Are you sure that only carriage-return will be accepted as a newline? Not the carriage-return *and* newline sequence `"\r\n"`? – Some programmer dude Apr 09 '14 at 07:13
  • Note that the suggested `sprintf_s` is part of Annex K, which is unpopular and has been removed from the C standard library and isn't considered standard C; see [this question](https://stackoverflow.com/q/50724726/4975230), if portability is a concern with your code (between OSes) you are probably better off using `sprintf`. IMO it's a bit heavy handed for Microsoft to treat using `sprintf` as an error. – jrh Oct 05 '19 at 14:37

2 Answers2

24

printf and its sister function sprintf are considered unsafe due to the amount of undefined behaviour they emit if used incorrectly.

Visual Studio is disabling these functions by default.

But, since they are part of the C++ standard library, you can use them. But Visual Studio will only allow you to do that if you include the line

#define _CRT_SECURE_NO_WARNINGS

before the relevant standard library headers are included.

Alternatively, include _CRT_SECURE_NO_WARNINGS in your project preprocessor settings (which is what I do).

(By the way, Visual Studio is emitting a very helpful error message in this instance. Do try to learn to interpret them.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I've clarified the placement location in my answer. For now, clobber it: put it in `stdafx.h`. – Bathsheba Apr 09 '14 at 07:01
  • 1
    I would argue that defining that macro is a bad idea. The warnings are there for a reason. @wireless_lab Please read about [`sprintf_s`](http://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx), it's not much you have to change to make it work. – Some programmer dude Apr 09 '14 at 07:02
  • @JoachimPileborg Is making an important point here. Do try to use safer features of C++. I too would be inclined to obey the compiler and use the safer `sprintf_s`. – Bathsheba Apr 09 '14 at 07:09
  • 1
    Defining `_CRT_SECURE_NO_WARNINGS` directive for my debug configuration works, it compiles, but defining it for my release build causes the error to persist. Any idea why? – James Wierzba Feb 26 '16 at 22:18
  • What's unsafe / undefined about `printf`? – Steve Summit Sep 08 '17 at 21:22
  • @SteveSummit: if you give the incorrect type, for example an `int` to a `%s` format, then it *might* crash the program. With `sprintf` the supplied buffer might not be large enough and overflow. – cdarke Sep 09 '17 at 07:30
  • @cdarke Ah, okay. To my mind, the possibility for mayhem with `sprintf` is qualitatively different from that for `printf`. `sprintf`'s glaring problem is easily fixed by switching to `snprintf` (or, if you're Microsoft, `sprintf_s`). `printf`'s problems, though rarer, are more fundamental, and I don't know how to fix them (short of disallowing `%n`). I don't know how a `printf_s` can be safer than `printf`, but that's my quibble with Microsoft, not you. Thanks for replying. – Steve Summit Sep 10 '17 at 13:11
3

For most of the errors in your question you need to add ws2_32.lib to the library that the project links to (in the VS project's Linker | Input | Additional Dependencies property).

Other answers/comments have addressed the sprintf() problem: either defined the macro _CRT_SECURE_NO_WARNINGS or use a version of sprintf() that is deemed safe by Microsoft, such as sprintf_s(). I wish MSVC had a standard version of snprintf() that can be used (maybe soon - they're adding quite a bit of C99 stuff to the toolchain).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760