Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing OCaml's gcc after installation

Tags:

gcc

ocaml

I installed OCaml via OPAM, and by default it uses gcc as the command to compile .c files. For instance, if I run ocamlopt -verbose file.c, I obtain:

+ gcc -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -g  
  -fno-omit-frame-pointer -c -I'/home/user/.opam/4.02.1+fp/lib/ocaml' 'test.c'

I'd like to change the GCC binary that is used by OCaml, for instance to replace it with gcc-5.1 or /opt/my-gcc/bin/gcc.

Is it possible to do so without reconfiguring and recompiling OCaml? I suppose I could add a gcc alias to a directory in the PATH, but I'd prefer a cleaner solution if there is one.

To check if gcc was not chosen based on a textual configuration file (that I could easily change), I searched for occurrences of gcc in my /home/user/.opam/4.02.1+fp directory, but the only occurrence in a non-binary file that I found was in lib/ocaml/Makefile.config, and changing it does nothing for the already-compiled binary.

like image 913
anol Avatar asked Oct 31 '25 05:10

anol


1 Answers

ocamlopt uses gcc for three things. First, for compiling .c files that appear on the command line of ocamlopt. Second, for assembling the .s files that it generates internally when compiling an OCaml source file. Third, for linking the object files together at the end.

For the first and third, you can supply a different compiler with the -cc flag.

For the second, you need to rebuild the OCaml compiler.

Update

Here's what I see on OS X when compiling a C and an OCaml module with the -verbose flag:

$ ocamlopt -verbose -cc gcc -o m m.ml c.c 2>&1 | grep -v warning
+ clang -arch x86_64 -c -o 'm.o' \
  '/var/folders/w4/1tgxn_s936b148fdgb8l9xv80000gn/T/camlasm461f1b.s' \
+ gcc -c   -I'/usr/local/lib/ocaml' 'c.c'
+ clang -arch x86_64 -c -o \
  '/var/folders/w4/1tgxn_s936b148fdgb8l9xv80000gn/T/camlstartup695941.o' \
  '/var/folders/w4/1tgxn_s936b148fdgb8l9xv80000gn/T/camlstartupb6b001.s'
+ gcc -o 'm'   '-L/usr/local/lib/ocaml' \
  '/var/folders/w4/1tgxn_s936b148fdgb8l9xv80000gn/T/camlstartup695941.o' \
  '/usr/local/lib/ocaml/std_exit.o' 'm.o' \
  '/usr/local/lib/ocaml/stdlib.a' 'c.o' \
  '/usr/local/lib/ocaml/libasmrun.a' 

So, the compiler given by the -cc option is used to do the compilation of the .c file and the final linking. To change the handling of the .s files you need to rebuild the compiler. I'm going to update my answer above.

like image 155
Jeffrey Scofield Avatar answered Nov 03 '25 06:11

Jeffrey Scofield