An error occurs when compiling a project with a dynamic library located in the lib folder. Help me figure out how to correctly specify the path. Thank.

lib / sum.c

#include "sum.h" int f1(int a, int b) { return a + b; } 

lib / sum.h

 int f1(int, int); 

The main file main.c is in the root of the folder, it contains the path to the file sum.h. How to correctly.

main.c

 #include <stdio.h> #include "./lib/sum.h" int main(void) { printf("%d", f1(10, 20)); getchar(); return 0; } 

Creating a dynamic library is great. It appears in the lib package.

gcc -shared ./lib/sum.c -o ./lib/libsum.dll

But when compiling a project with a dynamic library from the lib folder, an error occurs.

gcc main.c -o abc.exe -L ./lib/libsum.dll

  • -L lib -lsum course you tried? - 0andriy

1 answer 1

First, the -L switch specifies the directory in which the library will be searched, and not the path to the library itself. Secondly, the name of the linked library is indicated with the -l key. Thirdly, during the search the prefix "lib" and the extension ".dll" will be added automatically. Fourthly, it is better to specify the path to the header files to the compiler, and not in the code

 #include <stdio.h> #include "sum.h" int main(void) { printf("%d", f1(10, 20)); getchar(); return 0; } 

Summing up

 gcc -I./lib -L./lib -o abc.exe main.c -lsum 
  • Dear Sergey Gornostaev! The file abc.exe has appeared, but at startup a error is generated, which says that the library is not visible. - Anton
  • Because the operating system loader does not know where to look for it. On Windows, the dynamic library must either be in the same directory as the executable file, or in one of the directories included in the PATH environment variable. - Sergey Gornostaev
  • Dear Sergey Gornostaev! Now everything is clear! I made a stratified error, trying to call the library from the lib folder. Thanks you! - Anton