最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - Error with including pdcurses while compiling a C proj - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited Jan 21 at 6:03 Aatish Bagal asked Jan 20 at 12:02 Aatish BagalAatish Bagal 212 bronze badges 1
  • 6 If you get "fatal error: test-2.c: No such file or directory", then you're not in the directory where test-2.c resides. – AKX Commented Jan 20 at 12:04
Add a comment  | 

1 Answer 1

Reset to default 1

Static 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

发布评论

评论列表(0)

  1. 暂无评论