Why doesn't corebuild work with absolute paths? - ocaml

I was using ocamlc.opt like this:
ocamlc.opt -I /home/foo/bar/baz -c foo.ml
I thought I could do the same with corebuild:
ocamlc.opt -I /home/foo/bar/baz -c foo.ml
But that throws an error:
Failure:
Included or excluded directories must be implicit (not "/home/foo/bar/baz").

AFAIR, corebuild does some internal directory re-ordering and explicit paths break its re-ordering algorithm.

Related

Clang compilation : "Cannot execute binary file"

I am new to the clang++ compiler flags. I have an issue regarding compilation. Here is my cmd:
clang++ -I ../llvm-project/llvm/include -I ../llvm-project/clang/include
-I ../llvm-project/build/tools/clang/include -I ../llvm-project/build/include
-O3 -c $(llvm-config-7 --cxxflags)
projectToTestHeadersBuilding.cpp -o projectToTestHeadersBuilding
I am getting error:
./projectToTestHeadersBuilding: cannot execute binary file: Exec format error
After execution I have projectToTestHeadersBuilding file. But I can not run executable. Can you please help me to understand how to get executable, so I can run it using ./projectToTestHeadersBuilding ?
In your initial command you use the -c flag which makes clang output an object file. This is part of a compiled program but not a complete executable, in order to get the final executable you must perform a linking step, usually with other object files.
A simple compilation can be done as so:
clang++ projectToTestHeadersBuilding.cpp -c -o projectToTestHeadersBuilding.o
clang++ projectToTestHeadersBuilding.o -o projectToTestHeadersBuilding
./projectToTestHeadersBuilding
Generally we do not need to explicitly pass all those -I flags you have passed. If they are needed with your setup, add them to the commands I've included above.

the g++ command returns no such file or directory

I am new to programming; I'm starting a new job and I have to resume what the fellow before me did.
So I have to run a program called test.cpp in C++. This code contains a header called misc.hpp located in a subfolder of where test.cpp is called include.
When I open the terminal from where test.cpp is and run g++ test.cpp it tells me that:
test.cpp:4:19: fatal error: misc.hpp: No such file or directory.
I also tried the g++ test.cpp -I include/misc.cpp but same thing
Could you please help me?
The -I (upper-case i) option is to add a directory to search for header files. It's not for including source files.
So if the header file is include/misc.hpp then you should do
g++ test.cpp -Iinclude
-I include/misc.cpp doesn't work because:
The file is misc.hpp, not misc.cpp
You do not include files like this
-I is for include directories
So:
g++ test.cpp -I include

Linking flags order using QMAKE in QTCreator .pro file

I'm getting
[...]main.cpp:9: error: undefined reference to [...]
I already know the cause, but, I can't resolve it using QMAKE_ flags.
I need to set the -I flag in linking after the -L and before the -o flag, like this (I tested a handmade Makefile, and it's OK):
-L ./lib -lm [... other libs ...] -I ./include -o ./bin/program
But setting -I in QMAKE_LFLAGS, the -I flag goes in the beginning of g++ command.
Similar solution:
Referencing C functions in static library from C++
To instance a C lib from C++, you need to surround c headers with extern "C" { }

makefiles and linking a library in a different folder

I've search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.
I'm making a makefile for a school project and as part of my project I'm generating a static library and linking against it. The compiler throws an error when it gets to a header include in the static library. The code for that is just #include "StringUtil.h"
So in the makefile I have these relevant parts of code
LINKFLAGS=-Llib/ -lHTMLtools
bin : lib $(BIN_FILE)
lib : $(LIB_OBJ_FILES)
ar r lib/libHTMLtools.a $(LIB_OBJ_FILES)
$(BIN_FILE) : $(OBJ_FILES) #This is only obj/crawler.o for now
g++ -o bin/crawler obj/crawler.o
obj/crawler.o : src/crawler.cpp inc/crawler.h
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
so whenever I run the make bin command it generates lib.libHTMLtools.a as expected but when it gets to the
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
line it returns this error.
src/crawler.cpp:2:24: fatal error: StringUtil.h: No such file or directory compilation terminated.
Any help or advice will be appreciated!
In C++, library files are not enough. They are not used when compiling source code, but only when linking. To compile source file, you need to include headers. But the compiler need to know where to find it. Try adding -I utils/inc to your last line like this
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc -I utils/inc src/crawler.cpp

compiling a c++ program including mysql

I'm new to gcc, and trying to compile a c++ program which includes mysql.h using the command:
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
It works without issue, but I was wondering if someone could explain the arguments to me. I don't like using commands I don't understand.
Thanks
-o test means the output file is to be named "test".
test.cpp is your source file, of course.
-L/usr/include/mysql means to look for libraries in /usr/include/mysql, as well as in the usual link path. (It probably isn't finding any libraries here; my libmysqlclient.a is in the standard library directory /usr/lib. So I don't think you need this option.)
-lmysqlclient means to link with the mysqlclient library (actually named libmysqlclient.a)
-I/usr/include/mysql means to look for #include files in /usr/include/mysql, as well as in the usual include path.
try "man g++" for a full description of what the various options mean.
man gcc will give you the details of all these options.
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
g++ : the compiler
-o test : name the resulting binary "test"
test.cpp : your source file
-L : the directory to look in for libraries (that are specified by -l)
-l : named library to link against (looks for it in -L)
-I : the directory to look in for #included header files