I am trying to build code using ocamlc however, I got error
Error: Unbound module Stdlib
The /usr/lib/ocaml/ directory includes the following
stdlib.a stdlib.cma stdlib.cxma stdlib.p.a stdlib.p.cxma
OCaml version : 4.05.0
The standard library was renamed from Pervasives to Stdlib fairly recently. Your compiler is from before the change; i.e., the 4.05.0 compiler has a Pervasives module and no Stdlib module. The code you're trying to compile is most likely from after the change.
There's usually no reason to mention the name of the standard library because it is "pervasive". So you could try removing "Stdlib." wherever you see it. Or you could try renaming it to "Pervasives.".
If the code is much more recent than the 4.05.0 compiler you could encounter other problems, however.
The Stdlib module was introduced in 4.07.0. Before that it was called Pervasives. It seems like you're mixing installations.
A few commands that might help untangle it:
which ocamlc will tell you where the compiler is located.
ocamlc -config will tell you where the standard library is expected to be.
It is also strongly advised to use opam, the OCaml Package Manager, to manage OCaml installations as it allows you to switch between multiple installations and gives you access to the latest compiler as soon as it's released.
Related
The existing code is calling some sort of wx header file and my DEV C++ compiler just says there's no such file.
Code:
#include<wx/wx.h>
Compiler error:
[Error] wx/wx.h: No such file or directory
So my question is -
What is wx.h
How do I install it in my compiler so that I can use it?
Do I need to change my compiler or DEV C++ would do fine?
What is wx.h
It is the header file of a library. The GitHub project should have some instructions on how to fetch the dependencies and how to install them.
How do I install it in my compiler so that I can use it?
Normally you have to download the dependency, build it (following some instructions), and then you need to use the header in your project and link the library. The exact steps depend on each library and each compiler.
Do I need to change my compiler or DEV C++ would do fine?
In principle, no. Some libraries only work with some compilers, though.
Note that Dev-C++ is not a compiler, it is an IDE that comes with a port of GCC (as far as I know).
It seems that you are using WxWidgets framework but your compiler doesn't know where to find its headers, and apparently also libs which you would face with on a next step.
You, need to add to your compiler flags the output of wx-config --cxxflags. And also to your linker flags the output of wx-config --libs.
Assumption is of course that WxWidgets is installed on your PC
I'm trying to follow these instructions to compile a module that depends on another module which I've created: https://ocaml.org/learn/tutorials/modules.html
In my case, I have a module ~/courseFiles/chapter5/moduleA.ml and another module in ~/OCamlCommons/listMethods.ml. I have compiled listMethods.ml using ocamlopt -c listMethods.ml and this seemed to work, it produced a file listMethods.cmx.
The file moduleA.ml contains open ListMethods;;. Now with my terminal located at ~/courseFiles/chapter5 I ran ocamlopt -c moduleA.ml but the terminal returns
Error: Unbound module ListMethods
Now I can understand why it would do this, but the instructions at that site seem to indicate that what I've done is how you're supposed to do this. Presumably I need to pass in the location of either the script or executable files when compiling moduleA.ml, but I'm not sure what the syntax should be. I've tried a few guesses, and guessed about how I could do this with ocamlfind but I haven't succeeded. I tried looking for instructions on compiling modules located in different directories but didn't find anything (or anything I can make sense of anyway).
First of all, the toolkit that is shipped with the OCaml System Distribution (aka the compiler) is very versatile but quite low-level and should be seen as a foundation layer for building more high-level build systems. Therefore, learning it is quite hard and usually makes sense only if you're going to build such systems. It is much easier to learn how to use dune or oasis or ocamlbuild instead. Moreover, it will diverge your attention from what actually matters - learning the language.
With all that said, let me answer your question in full details. OCaml implements a separate compilation scheme, where each compilation unit could be built independently and then linked into a single binary. This scheme is common in C/C++ languages, and in fact, OCaml compiler toolchain is very similar to the C compiler toolchain.
When you run ocamlopt -c x.ml you're creating a compilation unit, and as a result a few files are produced, namely:
x.o - contains actually the compiled machine code
x.cmx - contains optimization data and other compiler-specific information
x.cmi - contains compiled interface to the module X.
In order to compile a module, the compiler doesn't need the code of any other modules used in that module. But what it needs is the typing information, i.e., it needs to know what is the type of List.find function, or a type of any other function that is provided by some module which is external to your module. This information is stored in cmi files, for which (compiled) header files from C/C++ is the closest counterpart. As in C/C++ the compiler is searching for them in the include search path, which by default includes the current directory and the location of the standard library, but could be extended using the -I option (the same as in C/C++). Therefore, if your module is using another module defined in a folder A you need to tell the compiler where to search for it, e.g.,
ocamlopt -I A -c x.ml
The produced objective file will not contain any code from external modules. Therefore, once you will reach the final stage of compilation - the linking phase, you have to provide the implementation, e.g., if your module X was using a module implemented in a file with relative path A/y.ml, and you have compiled it in that folder, then you need to specify again the location of the compiled implementation, e.g.,
ocamlopt -I A y.cmx x.cmx -o exe
The order is important, all modules used by a module should be specified before that module, otherwise, you will get the "No implementations provided" error.
As you can see, it is a pretty convoluted process, and it is really not worthwhile to invest your time in learning it. So, if you have an option, then use a higher-level tool to build your programs. If not sure, then choose Dune :)
I am curious about the C++ Modules TS. I have played a little around with Clang's implementation. Only one or two files or so at a time.
Now I would like to try something bigger than that. And I would like to use CMake.
Does someone know if there is some good way to use the Clang modules implementation with CMake or are there already some CMake modules which help me with this?
I would really like to know. Otherwise I have to consider using a different build system.
I would suggest you try build2:
https://build2.org/build2/doc/build2-build-system-manual.xhtml#cxx-modules
It supports modules for Visual Studio, gcc and clang. And for gcc there is a package that contains the standard library:
https://build2.org/pkg/hello/libstd-modules?f=full&q=library
Note that Microsoft implementation, as of Visual Studio 2017 update 4, is using the old syntax in the .ixx files (module xyz; instead of export module xyz; where the latter is what the last Modules TS draft mandates)
It shouldn't be any harder than configuring the proper command-line arguments. Since this feature has yet to be standardized and is different between the two known implementations (clang and MSVC -- gcc 7.2 documentation doesn't mention modules that I could find), I don't expect there are any CMake functions to handle anything.
AFAIK, the clang way of treating headers as special if they are in the module map file is lagging the current working draft for modules. IMO, it would be more useful to experiment with MSVC which is more closely tracking the WD for modules at this time. I don't know why g++ and clang are lagging here, they are usually early adopters. Perhaps it is because the specification is still in working draft stage and not yet a TS, I don't know.
I am trying to compile an OCaml file with the debugger flag -g with the following line within the file -- #use "file2.ml". Why does the file not compile as long as I have the use keyword in it? What exactly does the "#use" keyword do? Is there an alternative?
The directives starting with # are supported only in the toplevel, the OCaml interpreter, also known as a read-eval-print loop.
In the toplevel, #use causes the interpreter to read OCaml code from a text file. Afterward it continues to take commands interactively.
For compiled code, you should compile separately then link your modules together. If the code in file2.ml doesn't form a full module, you'll probably want to cut/paste it directly into the main file. OCaml doesn't have compile-time sourcefile inclusion like the C family has.
Update
Here's how to compile two OCaml files the old school way. You'll find there are those who say these ways are obsolete and that you should learn to use ocamlbuild. This may be correct, but I am old school at least for now.
$ ocamlc -o program file2.ml file1.ml
You have to list the .ml files in dependency order, i.e., a file has to be listed after any files it uses. This is already one reason to use a more powerful build tool.
Note that this does not behave the same as with #use, which loads all the top-level names of file2.ml into the global namespace. With separate compilation, names from file2.ml will be contained in a module named File2. If you want to call a function f defined in file2.ml, you should call it as File2.f.
TL;DR;
Use ocamlbuild to drive your compilation process.
ocamlbuild -clfags -g file1.native
Deeper into the woods
#use is a special directive in the OCaml toplevel, in other words in OCaml interpreter. This directives are special commands that are executed by the interpreter for some effects. They are not part of the language. Moreover, different interpreters have different directives. If you want to write an OCaml program, then you need to compile, since OCaml is a compiled language.
OCaml comes with a very descent infrastructure, here is a short guideline. First of all, there're two compilers, bytecode and native. Bytecode is slow (still much faster than Python or Ruby), but portable. Native compiles to native machine code and thus very fast. If you have an access to both compilers, then use the latter. One can say, that native compilation is slower, I would say that on a modern machines the difference is negligible.
The plethora of tools is pretty large, we have ocaml for interpreter, ocamlc for bytecode compiler, ocamlopt for native compilation, ocamldep for finding dependencies between modules, ocamldoc for compiling documentation, ocamlmktop for making your own interpreters, ocamlmklib to bundle libraries, ocamlrun to run bytecode. Also we have ocamlfind to find libraries on your system. Fortunately we also have One Tool to rule them all, One Tool to find them, One Tool to bring them all and in the darkness bind them.
Here comes ocamlbuild. This tool is a part of OCaml language distribution, and it knows everything about all 9 minor tools. So you don't really need to learn them in order to start programming in OCaml. ocamlbuild will find all dependencies, link libraries, and create for you anything you want, including shared libraries or your own interpreters. For example, to link with core library, just pass -pkg core option. Moreover, ocamlbuild has _tags file, that allows you to store your arguments and save some space on your command line. So, other way to always pass -g option (not a bad idea, btw) to compiler is to add the following to your _tags file:
debug : true
And since, you're adding this -g option, I suspect that you're interested in backtraces. If that is true, then don't forget to enable backtracing recording with either calling to Printexc.record_backtrace or by setting environment variable OCAMLRUNPARAM=b.
I came up with exactly the same problem, but the solution which I have found is to use the #include directive from the cppo preprocessor. You can include the preprocessor in the -pp switch on the OCaml compiler.
I am trying to make my autotools project in C++ link against library, that originates as C library (libsomelib.so), but also has bindings to c++ (libsomelib++.so). I ma trying to use PKG_CHECK_MODULES to check if this package is installed, and use autotools to link against it. However both libs come in one package (c++ version requires configure flag), and have only one .pc file, in which independently of configuration settings there is only line
Libs: -L${libdir} -lsomelib
without any mentioning of ++ version. There is also no separate ++.pc file that i noticed at other programs. Therefore automatic linking against ++ version is impossible. I thought about manually adding -lsomelib++ to linking flags, but that's rather ugly (and it will not work if somebody compiled it without --with-cxx flag). I could also test for it's existence by AC_SEARCH_LIBS, but since it's C++ library it's not so straightforward.
Is missing ++.pc file mistake of package distributor or is it some deeper idea, and i don't know how to use it?
If somebody is really qurious i will say that package in question is ossp-uuid.
Yes, the missing ++.pc usually hints towards an omission on behalf of the packager.
BTW: If simple (DCE) UUIDs are sufficient, you could consider e2fsprogs/util-linux's libuuid (in case you run this OS).