3

I'm looking for a C interpreter to use while making a simple C utility to avoid compiling all the time. I installed TCC as suggested here but I get warnings and errors. How do I run TCC correctly?

$ tcc -run hello.c
.../usr/include/sys/cdefs.h:81: warning: #warning "Unsupported compiler detected"
#if !defined(__GNUC__) || __GNUC__ < 4
#warning "Unsupported compiler detected"
#endif

Setting __GNUC__ causes an error later on:

tcc -D__GNUC__=4 -run hello.c
.../usr/include/i386/_types.h:98: error: ';' expected (got "__darwin_va_list")
#if (__GNUC__ > 2)
typedef __builtin_va_list   __darwin_va_list;   /* va_list */
#else
typedef void *          __darwin_va_list;   /* va_list */
#endif

My environment:

~$ gcc --version
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0

If TCC is not fit for macOS, please suggest a C interpreter that plays nicely.

forthrin
  • 2,709
  • 3
  • 28
  • 50

3 Answers3

0

For sure one that will work is CERN's Cling or any other based on LLVM/Clang, since that is what Apple uses in macOS.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • Huge download, overwhelming `--help` and very technical documentation. Not easy to get into... – forthrin Mar 27 '18 at 11:50
  • @forthrin: If you need a minimal solution based on LLVM/Clang, you can also create your own tiny interpreter: see https://github.com/llvm-mirror/clang/tree/master/examples/clang-interpreter However, I guessed you preferred something readily available, which is why I suggested Cling. – Acorn Mar 27 '18 at 12:01
0

The block comment immediately above the line your first message complains about is

/* This SDK is designed to work with clang and specific versions of
 * gcc >= 4.0 with Apple's patch sets */
#if !defined(__GNUC__) || __GNUC__ < 4
#warning "Unsupported compiler detected"
#endif

which is pretty clear - you do need gcc or clang. Fortunately both of those compilers are really easy to install - use https://www.macports.org.

I wouldn't bother with a C interpreter - it's not an interpreted language.

James McPherson
  • 2,476
  • 1
  • 12
  • 16
  • MacOS comes with GCC/clang out of the box (updated original post). Is it a shortcoming of TCC that it doesn't pick this up automatically? – forthrin Mar 27 '18 at 11:42
  • Clang, gcc and tcc are all different compilers. The problem is that tcc tries to use standard library bundled with OS, which is not compatible with tcc. – el.pescado - нет войне Mar 27 '18 at 11:47
  • Well, actually by using `-run` my (simple) C code *does* interpret, so I could just remove the warning and see how far I get. – forthrin Mar 27 '18 at 11:51
  • 1
    @forthrin apparently you don't know the differences between *compiled* and *interpreted*. The name TCC comes from [*"Tiny C Compiler"*](https://bellard.org/tcc/). It is a compiler, not an interpreter. The `-run` command line option tells it to launch the compiled program into execution (if the compilation succeeds). – axiac Mar 27 '18 at 12:14
  • I see! I do know the difference, but clearly I've misread the referenced article which seemed to speak of it as an interpreter. Well, if there is no such thing as a (lightweight!) "C interpreter", then I may as well stay with GCC. – forthrin Mar 27 '18 at 13:08
  • @forthrin: lightweight C interpreters exist, but if you want to use the system headers (instead of, say, a minimal C standard library or no library at all), then you will probably want a proper C compiler -- which, for macOS, leaves you back in the Clang/LLVM realm, whether as a compiler or as an interpreter like Cling or your own. – Acorn Mar 27 '18 at 14:46
  • Please see https://stackoverflow.com/questions/584714/is-there-an-interpreter-for-c for a list of C interpreters. – dstromberg Oct 30 '20 at 03:56
0

First of all, tcc is not an "interpreter" it is a very fast compiler that can be used to compile and run your C code as if it were script.

Secondly, the "Unsupported compiler detected" warning is just that, a warning. I get that warning all the time and my code still compiles and runs no problem. If the warning bothers you, you can simply run tcc with the -w option to suppress the warnings (probably only advisable if you are re-running a file that you already know has no issues).

For example, if you are running the C code as if it's a script using the tcc shebang line, you can change it to this

#!/usr/local/bin/tcc -w -run

There can be a couple of other issues when running tcc on macOS. The main one is missing include files. On macOS the include files may not be installed to /usr/include/. See this question for the fix. Once Xcode had properly installed the headers, I still needed to update my environment variable to get tcc to find them.

 export C_INCLUDE_PATH="/usr/include:$C_INCLUDE_PATH"

You can see where tcc is looking for header by running tcc -vv.

Dharman
  • 30,962
  • 25
  • 85
  • 135
nalyd88
  • 4,940
  • 8
  • 35
  • 51