Force mac to use GCC, not clang - c

I have a program written on Linux in GNU C. The program compiles with GCC. I have an install shell script,
gcc -o program program.c -Wall -pedantic -std=gnu11 -lm -fopenmp
which works greatly.
The problem is with Mac. For some reason, Mac sees gcc and instead uses clang even though GCC is installed and install.sh explicitly says gcc. clang doesn't work with omp.h The problem is that clang can't use omp.h and solutions offered http://releases.llvm.org/3.7.0/tools/clang/docs/ReleaseNotes.html#openmp-support don't work, and omp.h may not work in mac at all Enable OpenMP support in clang in Mac OS X (sierra)
I never use macs, and because of this nonsense I never plan to. However, some people using my program want to use Mac, so I have to deal with this.
I've tried various shell script modifications, but none of them work, mac insists on using clang and won't use gcc
I need to do one of two things which I don't know how to do:
1) force Mac to use gcc (which it refuses to do now)
2) get clang to use omp.h in mac (which from other answers on Stack Overflow, looks impossible)

Your second option (get clang to use omp.h) is not impossible (any more). From my answer here:
Try using Homebrew's llvm:
brew install llvm
You then have all the llvm binaries in /usr/local/opt/llvm/bin. To compile the OpenMP Hello World program, for example, type
/usr/local/opt/llvm/bin/clang -fopenmp -L/usr/local/opt/llvm/lib omp_hello.c -o hello
You might also have to set the CPPFLAGS with -I/usr/local/opt/llvm/include.

Related

Run precompiled C program

I am currently working on a program that is encrypting a text file. I made it in Turbo C++, using C language, but my main problem is that:
I need to run turboc++, in order for my .exe program to run. Does anybody here know a way to compile it and run it as stand-alone program?
TurboC++ is an obsolete compiler for an obsolete variant of C++ or of C. Use a recent compiler (such as GCC 8 or Clang 7; both are open source so freely available) for recent C11 or C++14 (or C++11) standards. Get rid of TurboC++ since it is obsolete (and is not a good compiler, compared to other existing ones).
If using GCC, you'll compile your C file foo.c using gcc -Wall -g -O foo.c -o foo. If using Clang, you'll compile with clang -Wall -g -O foo.c -o foo. Don't forget to enable all warnings and debug info. You'll get an executable foo which can be run without having the source code. That executable is specific to your operating system and to your instruction set architecture.
I need to run turboc++, in order for my .exe program to run.
With any good enough C or C++ compiler, you don't need the compiler to run the executable it is producing.
Don't confuse a compiler with the IDE or source code editor you'll use to write C or C++ source code. All C or C++ compilers I heard of are command line programs, that might be run from a terminal, an IDE, a good source code editor (such as emacs or vim).
If your source is in C++, that is bar.cc, use g++ -Wall -g -O bar.cc -o bar or clang++ -Wall -g -O bar.cc -o bar
Adapt these compilation commands (I'm giving those for Linux) to your operating system. On Windows, executables have a file path ending with .exe.
Of course, both GCC and Clang are able to compile and link a program made of several translation units. Learn to use some build automation tool, such as make or ninja. Such tools are driving compilation and linking commands.
If you are learning to program in C++, be aware that it is a very difficult programming language (you'll need years of efforts to master it). And notice that Linux is a very developer-friendly operating system, mostly made of free software whose source code you could study. That is why I recommend Linux for those learning C++ or C.
PS. If your teacher requires TurboC, I do recommend to have a polite discussion with him, suggesting to make your homework with GCC or Clang.

How to cross compile a C SDL program for Windows on Linux?

I have a perfectly working C program using SDL that I can compile with
gcc main.c -o program `sdl-config --libs` -lSDL
The program is simple, it comes up with a black window and waits for the user to close it. No problems or errors. I have MinGW installed and I'm trying to use x86_64-w64-mingw32-gcc to cross-compile it; I would like to know how to cross-compile this for Windows on Linux and include the necessary libraries.
You can invoke MinGW’s GCC like your Linux version, but you might need to replacesdl-config with some manual configuration like so:
x86_64-w64-mingw32-gcc -Ipath/to/SDL/headers \
-Lpath/to/SDL/libs \
-o program main.o \
-lSDL -lSDLmain
You will need to download SDL.lib and SDLmain.lib. I believe they are part of the development zip. (Edit: as noted in the comments, you might not need to use the libs but can just use the DLLs.)
This might pop up a console window when running. Pass in the -mwindows flag to target the Windows subsystem instead.

Settings GCC (ARM-Linux) in Eclipse Mars for 'C project

I am experimenting with cross compiling for ARM-Linux under Windows using Eclipse-Mars. My set-up is as follows:
Win10 x64
Eclipse Mars.2 Release (4.5.2)
GNU Toolchain for RaspberryPi (SysGCC)
Target platform: RaspberryPi2 running Raspian
Source project in 'C (not C++) using Linux Threads (pthreads)
I have knocked up a small 'C project using 'pthreads' which compiles under Eclipse and runs successfully on the Pi.
My problem is that Eclipse shows a number of errors in its Problems TAB to do with Linux's threads:
Type 'pthread_cond_t' could not be resolved
Type 'pthread_mutex_t' could not be resolved
Type 'pthread_t' could not be resolved
I have Eclipse as:
Cross GCC Compiler settings to use g++ with -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0.
Cross G++ Compiler settings to use g++ with -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0.
In other words both are the same.
If I rename my source files from foo.c to foo.cpp and recompile, then the Eclipse errors disappear!!!
This implies that Eclipse's C++ settings are correct, but its 'C settings are not.
Can anybody suggest anything for me to try?
g++ ist a C++ compiler, so you might try to use other settings for the Cross GCC Compiler, eg. gcc.
Also make sure you are linking the pthread library.

How to Self-Host Clang?

Can anyone tell me how to compile the Clang compiler into LLVM bytecode (that is, self-host Clang)? The reason I want to do this is so that I can take the resulting LLVM bytecode and then use Emscripten to produce a C to Javascript compiler.
You can get clang to output into LLVM bytecode by using the -emit-llvm command-line flag, along with the -c flag. (If you use the -S flag instead of -c, you get a textual representation of the LLVM bytecode.) You don't need to compile clang into LLVM bytecode for that to work.
If you want to try to run clang itself inside a browser, then you will need to compile all of clang into LLVM bytecode, and then link the object files together using llvm-link. Then you'll need to figure out how to give the compiled compiler access to the system header files it needs. I don't know if there is a build option for all that, but I haven't ever seen anything in the ./configure options for that, so I suspect not. But it's possible that it exists.

GNU Autotools with TCC

I recently discovered the Tiny C Compiler. For the project that I'm currently working on, performance is not a real issue, but file size is, making TCC ideal. I'm using Autotools as a build manager, and I figured that using TCC would be as simple as ./configure CC=tcc.
However, this returns checking whether the C compiler works... no. In config.log, it says configure: exit 77.
Despite all of this, setting CC=clang works fine. Is there any way to get Autotools to use TCC?
The issue appears to have been the fault of my CFLAGS. While TCC was normally able to compile programs with them, Autotools seems to have thought otherwise. Setting CFLAGS="" resolved the issue.
For future reference, my CFLAGS are -march=native -mtune=native -O2 -pipe -fstack-protector --param=ssp-buffer-size=4.
iirc tcc does not produce tiny executables - it is tcc itself that is tiny. Perhaps you're looking for gcc -Os?

Resources