nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.41k stars 1.47k forks source link

".h" files are not found for dependencies #10971

Open pb-cdunn opened 5 years ago

pb-cdunn commented 5 years ago

When I build/install a particular module, everything is fine. I have a hybrid package, with .nim, .c, and .h files, installed as both a library and as binaries. That works.

The problem comes when I try to use it from another package.

% nimble install --nimbleDir:$N --verbose -y
...
        ... gcc -c  -w -g3 -O0 -O3 -fno-strict-aliasing  -I/mnt/software/n/nim/0.19.9/Nim/lib -I/localdisk/scratch/cdunn/repo/nim-falcon/src/falcon -o /home/UNIXHOME/cdunn/.cache/nim/example_r/DBX.c.o /localdisk/scratch/cdunn/repo/nim-falcon/nimbleDir/pkgs/daligner-0.0.0/daligner/DBX.c

# good!

        ... gcc -c  -w -g3 -O0 -O3 -fno-strict-aliasing  -I/mnt/software/n/nim/0.19.9/Nim/lib -I/localdisk/scratch/cdunn/repo/nim-falcon/src/falcon -o /home/UNIXHOME/cdunn/.cache/nim/example_r/daligner_dbx.c.o /home/UNIXHOME/cdunn/.cache/nim/example_r/daligner_dbx.c

        ... Error: execution of an external compiler program 'gcc -c  -w -g3 -O0 -O3 -fno-strict-aliasing  -I/mnt/software/n/nim/0.19.9/Nim/lib -I/localdisk/scratch/cdunn/repo/nim-falcon/src/falcon -o /home/UNIXHOME/cdunn/.cache/nim/example_r/daligner_dbx.c.o /home/UNIXHOME/cdunn/.cache/nim/example_r/daligner_dbx.c' failed with exit code: 1
        ... /home/UNIXHOME/cdunn/.cache/nim/example_r/daligner_dbx.c:10:17: fatal error: DBX.h: No such file or directory
        ...  #include "DBX.h"
        ...                  ^

Apparently, Nim fails to copy the *.h files into the compilation directory -- for dependencies only. The C code DB.c compiles fine because in C, #include "foo.h" looks in the directory of the source-file. But the wrapper daligner_dbx.c, from dbx.nim (which has {.compile: "DBX.c".}).

See the problem?

Again, when I build the package that has the .h files, everything works, but that's because Nim automatically adds -I for the current package, e.g.:

gcc -c  -w -g3 -O0 -O3 -fno-strict-aliasing  -I/mnt/software/n/nim/0.19.9/Nim/lib -I/localdisk/scratch/cdunn/repo/nim-falcon/repos/nim-DALIGNER/src/daligner -o /home/UNIXHOME/cdunn/.cache/nim/LA4Falcon_r/daligner_dbx.c.o /home/UNIXHOME/cdunn/.cache/nim/LA4Falcon_r/daligner_dbx.c

See -I/localdisk/scratch/cdunn/repo/nim-falcon/repos/nim-DALIGNER/src/daligner? That's the key. I think the equivalent needs to be added when building dependencies.

pb-cdunn commented 5 years ago

https://forum.nim-lang.org/t/4768

pb-cdunn commented 5 years ago

Best solution so far:

from os import DirSep
from strutils import rsplit
const thisdir = system.currentSourcePath.rsplit(DirSep, 1)[0]
{.passC: "-I" & thisdir.}
genotrance commented 4 years ago

You found the solution - tell the C compiler where the include files are. Why should Nim do this automatically?

pb-cdunn commented 4 years ago

Nim already does it automatically, but only for the current module. The behavior is different when I module is used from another Nim module.

The reason this should work for dependencies is the same as the reason it was already made to work for the current (top) module.

cdunn2001 commented 3 years ago

This remains a problem. I still don't understand the logic. Module A uses module B, and module B has .c and .h files. Module A does not know that module B needs those extra files. But module B compiles fine itself.

You can see my current solution here:

You can test this easily. Clone nim-edlib and switch to cpp branch. Then cd apps; make. That works. Then clear the cache and the .exe, remove the -I line from edlib.nim, and try again.

~/.cache/nim/aligner_d/aligner.nim.cpp:10:10: fatal error: 'edlib.h' file not found
#include "edlib.h"
         ^~~~~~~~~
timotheecour commented 3 years ago

related to https://github.com/nim-lang/RFCs/issues/510 ; please check whether it's an exact dup or not

pb-cdunn commented 3 years ago

Hmm. Not quite the same, but yes, related.