I have a test-2.c file in which I have included curses.h
#include "pdcurses/curses.h"
The pdcurses folder is in the same directory as my .c file
How do I compile it using gcc?
I want to compile this into a .exe file on windows. I tried these commands but none of them worked
gcc test-2.c -I \pdcurses -L pdcurses\wincon -lpdcurses -o test-2.exe
gcc -o test-2.exe test-2.c -Lpdcurses -lcurses
Errors after executing these commands
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lcurses: No such file or directory collect2.exe: error: ld returned 1 exit status
test-2.c exists in
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project
pd curses folder also exists in the same folder, for reference, curses.h location -
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project\pdcurses
libpdcurses.a location -
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project\pdcurses\wincon
I have a test-2.c file in which I have included curses.h
#include "pdcurses/curses.h"
The pdcurses folder is in the same directory as my .c file
How do I compile it using gcc?
I want to compile this into a .exe file on windows. I tried these commands but none of them worked
gcc test-2.c -I \pdcurses -L pdcurses\wincon -lpdcurses -o test-2.exe
gcc -o test-2.exe test-2.c -Lpdcurses -lcurses
Errors after executing these commands
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lcurses: No such file or directory collect2.exe: error: ld returned 1 exit status
test-2.c exists in
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project
pd curses folder also exists in the same folder, for reference, curses.h location -
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project\pdcurses
libpdcurses.a location -
C:\Users\aatis\OneDrive\Onenote\first-year\C\course-project\pdcurses\wincon
1 Answer
Reset to default 1Static lib
gcc -c pdcurses/curses.c -o pdcurses/curses.obj
# ^ You can also type ".o"
If you have more than one source file, compile them also as curses.c
ar rcs pdcurses/libcurses.lib pdcurses/curses.obj # And your other modules
# ^ You can also type ".a"
gcc -Lpdcurses -lcurses test-2.c -o curses.exe
Dynamic lib
This was a static lib, if you need a dynamic one, you need to do the following:
gcc -shared -o pdcurses/libcurses.dll pdcurses/curses.c # And another modules
# ^ Or ".so"
gcc test-2.c -Lpdcurses -lcurses -Wl,-rpath=pdcurses -o test.exe
# ^ Without space char
Explanation
Static
First we compiled the source files into object files using gcc
Next we linked all the object files into a lib using ar
The library name must start with lib
Finaly, we made an executable file also using gcc
-L
- specifies the directory where the lib will be located
After this flag, you must write the path to the directory without spaces after flag
-l
- this is the library itself
You need to write lib name, also without spaces after the flag
Do not include the lib
prefix when using the -l
flag
This is exactly what the -L
flag is for
Dynamic
First we made the lib
-shared
- needed to create specifically a library
Name also must start with lib
Next we made an executable file
-L
& -l
work same
-Wl,-rpath=
- Path where the lib should be located when executing the file
after -rpath=
You must write the path
test-2.c
resides. – AKX Commented Jan 20 at 12:04