Thread overview
Directly compiling a D program with other libraries
Mar 13, 2023
Jeremy
Mar 13, 2023
Mike Parker
Mar 13, 2023
Jeremy
March 13, 2023

Hello, I am new to this forum and to D.

I am trying to compile a basic D program with libraries (requests which requires cachetools and automem) without using dub. I have never used dub before, only a compiler.

The folders containing the libraries are in the same folder as main.d, the file I am trying to compile, and the command I am using to compile is ldc2 -I. main.d.

When I compile my program, I just get linker errors such as:

/usr/lib/gcc/x86_64-pc-linux-gnu/12/../../../../x86_64-pc-linux-gnu/bin/ld: main.o: in function `_Dmain':
main.d:(.text._Dmain+0x2f): undefined reference to `_D8requests10getContentFNcAyaZSQBd7streams__T6BufferThZQk'

Does anyone have any advice on how to solve my problem?

March 13, 2023

On Monday, 13 March 2023 at 05:05:27 UTC, Jeremy wrote:

>

Hello, I am new to this forum and to D.

I am trying to compile a basic D program with libraries (requests which requires cachetools and automem) without using dub. I have never used dub before, only a compiler.

The folders containing the libraries are in the same folder as main.d, the file I am trying to compile, and the command I am using to compile is ldc2 -I. main.d.

When I compile my program, I just get linker errors such as:

/usr/lib/gcc/x86_64-pc-linux-gnu/12/../../../../x86_64-pc-linux-gnu/bin/ld: main.o: in function `_Dmain':
main.d:(.text._Dmain+0x2f): undefined reference to `_D8requests10getContentFNcAyaZSQBd7streams__T6BufferThZQk'

Does anyone have any advice on how to solve my problem?

That's a linker error, meaning the missing symbol isn't available to link into the executable. You need to compile the source of all the libraries you use and make sure the resultant binaries are available for the linker to link into the executable.

The -I switch you've passed tells the compiler where to find imported modules. The compiler needs parse them to know which symbols are available for you to use when it's compiling your code. (The current working directory is the default anyway, so you don't need to pass -I. for that.)

By default, the compiler does not compile imported modules. If you add -i to the command line, then it will compile all of the modules you import (as long as they're in the -I path), excluding the DRuntime and Phobos modules. It will then also pass all of the compiled object files to the linker, so then your linker error should go away.

However, when you choose not to use dub, you need to also ensure that you are accounting for any special compiler flags the libraries you use may require (for example, specific -version values). If they're configured to compile as static or shared libraries, it may be easier just to store the source for each of them outside of your project's source tree, use dub to build each of them, and then pass the compiled libraries to the compiler when you build your program. In that case, you wouldn't use -i. Just make sure that -I is correctly configured in that case.

March 13, 2023
>

That's a linker error, meaning the missing symbol isn't available to link into the executable. You need to compile the source of all the libraries you use and make sure the resultant binaries are available for the linker to link into the executable.

The -I switch you've passed tells the compiler where to find imported modules. The compiler needs parse them to know which symbols are available for you to use when it's compiling your code. (The current working directory is the default anyway, so you don't need to pass -I. for that.)

By default, the compiler does not compile imported modules. If you add -i to the command line, then it will compile all of the modules you import (as long as they're in the -I path), excluding the DRuntime and Phobos modules. It will then also pass all of the compiled object files to the linker, so then your linker error should go away.

Using -i solved my problem, thank you very much!

>

However, when you choose not to use dub, you need to also ensure that you are accounting for any special compiler flags the libraries you use may require (for example, specific -version values). If they're configured to compile as static or shared libraries, it may be easier just to store the source for each of them outside of your project's source tree, use dub to build each of them, and then pass the compiled libraries to the compiler when you build your program. In that case, you wouldn't use -i. Just make sure that -I is correctly configured in that case.

Okay, thanks.