0

How to write C program in VS2012 without using Microsoft specific extension C library?

For Example scanf_s() is Microsoft specific implementation. But if we use it we can't compile the code in Linux.

So should we use Linux VM in parallel the compile the code in gcc. OR we can setup gcc on windows. OR is there any better option or tweak in VS 2012 itself to achieve this?

  • Is this what you're looking for? http://stackoverflow.com/a/5060359/2609288 – Baldrick Oct 28 '13 at 06:30
  • Possible duplicate of http://stackoverflow.com/questions/5060034/vs2010-c-and-c-enforce-ansi-compliance-for-linux-gcc-compatibility – Baldrick Oct 28 '13 at 06:35
  • Writing protable code is not trivial and the efforts you have to take strongly depend on which functionality you are after. The minimal advise I could give without knowing more about what you really need to build is to stick to the functions defined by the C Standard Library. Using C99 might not be too conservative: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf – alk Oct 28 '13 at 07:34
  • sorry for coming back late. – user2926764 Nov 30 '13 at 15:26
  • @Baldrick i have set up "Disable Language Extension" as Yes from project properties->c/c++->Language. But still see 'scanf_s("%d", &n);' compiles fine VS2012. Did i missed something? or 'scanf_s()' ANSI compliant? – user2926764 Nov 30 '13 at 15:33

1 Answers1

2

Setting "Disable Language Extensions" just removes certain language features. A list of these features is here here.

However, extra functions provided by Microsoft aren't language features - they are just optional functions that are there if you want to use them. They are not disabled by that setting. The secure versions of standard functions, suffixed by _s, are in this category.

Having said that, if you navigate to the definition of scanf_s in the header, you can see that Microsoft have provided a way of disabling this particular family of functions.

If you define the following in your code before your header #includes

#define __STDC_WANT_SECURE_LIB__ 0

then scanf_s will no longer compile.

If you want to achieve this through your compiler switches, go to your project properties, and find the Preprocessor definitions. Add the following definition to the end:

__STDC_WANT_SECURE_LIB__#0

You can probably find similar ways to disable other additional Microsoft functions which are not part of 'standard' C.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
  • not a very user friendly way, but that's the price need to pay for using MS editor. Probably easy way will be , not tested, to use Eclipse editor for getting 'standard' c code. – user2926764 Dec 17 '13 at 12:12
  • @user2926764 It's not about editor, it's about compiler, or more strictly standard library. – Mariusz Jaskółka Jun 28 '19 at 06:36