0

Because I am not sure if it is okay to post the sample code from the book here, please let me explain it with normal words.

I am learning C and tried some codes in my textbook which contained strerrorlen_s() and strerror_s(). When I compiled the code with clang on macOS, I was encountered with errors saying that implicit declaration of function 'strerror_s'/'strerrorlen_s' is invalid in C99.

It seemed that the author of the book compiled the code with visual C++, and I guessed that I got the errors because I used different compiler. Did I guess right?

Please let me know if there is no legal problem with posting sample codes from the textbook, and you want me to do so. I'll add the codes.

Thank you for your time.

JUSEOK KO
  • 69
  • 7

2 Answers2

2

You're basically correct.

strerrorlen_s() and strerror_s() are C11 functions: https://en.cppreference.com/w/c/string/byte/strerror

Your Visual Studio compiler supports them, your MacOS compiler apparently doesn't.

SUGGESTION: Google for "C11 support MacOS" to see what compilers support C11 for your particular environment. For example:

How to compile C++ with C++11 support in Mac Terminal

As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.

clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.

xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

It's less likely to be a compiler issue than you using a different version of C++ than the author. Re-read the beginning of the book. There's gonna be an introduction or a prefix which indicates what version of C++ the author using.

PJacobs
  • 23
  • 3