I am trying to compile a windows .c file on linux using the following command:
wine gcc.exe x.c -o x.exe -lws2_32
And I get this error.
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:(.text+0x104): undefined reference to `WinMain#16'
However, when using gcc.exe with the -shared attribute, the error gone.
wine gcc.exe -shared x.c -o x.exe -lws2_32
I tried viewing the help page of gcc.exe but can not find anything related to "-shared" argument
What does this argument do ?
-shared will make a shared object from the code, rather than an executable.
An executable would need a main function as an entry point, hence the undefined reference error you saw.
The shared object can be linked with other objects to make an executable.
See here or here
You first line is actually almost correct. (Doctorlove told you that shared is for making shared objects, libraries, which is true.)
You have to add:
wine gcc.exe -mwindows x.c -o x.exe -lws2_32
The -mwindows option is to tell GCC that you want to link a graphical Windows program, not a console program. Of course, your program must also contain a main() or WinMain() function, or you will get the same error message again.
As a side note, you may want to know that you don't have to use wine to cross compile with Mingw. There are mingw cross compilers for Linux. On Debian and Ubuntu you install it with apt-get install mingw32.
Related
I'm running a Debian (Buster) container and my goal is to compile a small program I wrote which relies on libgit2. First, I was installing libgit2 via the libgit2-dev package and my Makefile had the following:
gcc -O2 -fpic -shared -I /usr/local/include -lgit2 -o output.so my_app.c
However, I'd rather have a "cleaner" environment and install libgit2 via the libgit-27 which, AFAIK, only installs the shared object binary instead of also including the development files like libgit2-dev does.
Using find I can find where the .so file is installed into:
$ find / -name "*git2*" -print 2>/dev/null
/usr/lib/x86_64-linux-gnu/libgit2.so.0.27.7
/usr/lib/x86_64-linux-gnu/libgit2.so.27
/usr/share/doc/libgit2-27
/var/lib/dpkg/info/libgit2-27:amd64.list
/var/lib/dpkg/info/libgit2-27:amd64.symbols
/var/lib/dpkg/info/libgit2-27:amd64.md5sums
/var/lib/dpkg/info/libgit2-27:amd64.shlibs
/var/lib/dpkg/info/libgit2-27:amd64.triggers
and I've been trying several combinations of linking this .so with gcc like:
gcc -O2 -fpic -shared -L /usr/lib/x86_64-linux-gnu/ -libgit2.so.27 -o output.so my_app.c
but so far I always get the following error:
my_app.c:1:10: fatal error: git2.h: No such file or directory
#include <git2.h>
^~~~~~~~
compilation terminated.
I understand this is a glaring lack of knowledge on how C compilation works. My two questions are:
Is it possible to compile my program by just relying on the libgit2-27 Debian Buster package instead of libgit2-dev? If not, why?
If yes, an example and explanation would be appreciated!
I have a perfectly working C program using SDL that I can compile with
gcc main.c -o program `sdl-config --libs` -lSDL
The program is simple, it comes up with a black window and waits for the user to close it. No problems or errors. I have MinGW installed and I'm trying to use x86_64-w64-mingw32-gcc to cross-compile it; I would like to know how to cross-compile this for Windows on Linux and include the necessary libraries.
You can invoke MinGW’s GCC like your Linux version, but you might need to replacesdl-config with some manual configuration like so:
x86_64-w64-mingw32-gcc -Ipath/to/SDL/headers \
-Lpath/to/SDL/libs \
-o program main.o \
-lSDL -lSDLmain
You will need to download SDL.lib and SDLmain.lib. I believe they are part of the development zip. (Edit: as noted in the comments, you might not need to use the libs but can just use the DLLs.)
This might pop up a console window when running. Pass in the -mwindows flag to target the Windows subsystem instead.
I want to learn OpenGL programming with GLFW library. But I have problem when compiling the program. I followed tutorial on http://www.glfw.org/docs/latest/quick.html and copied the full program. I download the 64-bit version library on http://www.glfw.org/download.html.
I changed the first line from include <GLFW/glfw3.h> to include "GLFW/glfw3.h". I've put the GLFW folder inside the folder along with glfw3.dll, glfw3dll, and libglfw3.a
I'm running on Windows 7. This is my gcc version
gcc --version
gcc (tdm64-1) 5.1.0
I use this command for compiling
gcc test.c -Llib-mingw/ -lglfw3 -lopengl32 -lgdi32
But I got this error
cannot find -lglfw3
What should I do to make it work?
If you are still having problems with this compilation/linking problem, try set the glfw full lib folder path in gcc or g++ lib path with -L parameter, as simplified below. Double quotes and non-relative paths should work.
g++ "-L<path_to_glfw>\\glfw\\lib-mingw" -o <executable>.exe <.o files list> -lopengl32 -lglfw3 -lgdi32
Two notes:
There is no need to pass .a extension. MinGW g++/gcc compiler will automatically understand filename.
If you are using the 64-bit version of glfw, switch -L parameter to \glfw\lib-mingw64 (or respective 64-bit library location folder)
I am working on a program which uses ncurses which will be used on embedded systems. Since these systems won't have ncurses installed I need to statically link the library. However if I try to build it like this
gcc -static ncurs.c -o ncurs -l:libncurses.a
or
gcc -static ncurs.c -o ncurs -lncurses
I get a ton of errors like this:
(.text+0x48): undefined reference to `SP'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libncurses.a(lib_slktouch.o): In function `slk_touch':
normal compilation works fine.
I have searched for hours but I can't find any good information...
platform of development is stripped down debian system.
I guess you may need additional library, can you try -lncurses -ltinfo.
You can check on your system what the linker library flags for ncurses using command
pkg-config --static --libs ncurses
on my system, I got
-lncurses -ltinfo
try adding -ltinfo to the end of your commandline
I am running Ubuntu 12.04 and I'm currently working on a project involving C, OpenGL, a teapot and input methods.
The problem started when I decided to have arrow keys as input. I checked to see the key codes for arrow keys but all of the arrows return 0. I looked up how to get this to work and I found conio.h. Unfortunately, it is an old DOS header that is not available for Linux. Then I found a substitute called ncurses.
After installing the necessary libraries, by following the build instructions closely, I #included curses.h in my main.c source. When I first tried to compile using gcc, I got the following errors:
main.o:main.c:function _Key: error: undefined reference to 'stdscr'
main.o:main.c:function _Key: error: undefined reference to 'wgetch'
main.o:main.c:function _Key: error: undefined reference to 'stdscr'
main.o:main.c:function _Key: error: undefined reference to 'wgetch'
I found a fix by adding -lncurses to the makefile like so:
SOURCES=main.c
main: main.o
gcc -lm -lGL -lGLU -lglut -lncurses main.o -o main
main.o: main.c
gcc -lm -lGL -lGLU -lglut -c main.c
But I was greeted by another error:
/usr/bin/ld: error: cannot find -lncurses
As well as the previous errors.
I have spent the last 2 days searching both the Ubuntu forums and StackOverFlow. Any help would be appreciated.
P.S. I don't know if this is important but when I try to run /usr/bin/ld I get this error:
ld: fatal error: no input files
For anyone with the same problem I had: I was missing the 32 bit libraries; I was compiling 32 bit on a 64 bit server which was missing the lib32ncurses5-dev package.
On Ubuntu I simply ran:
sudo apt-get install lib32ncurses5-dev
First off, you should put the libraries after the object file when linking. And not have them at all in the compilation of of the source file.
After that, if ncurses is not installed in a standard search folder you need to point out to the linker where it is, this is done with the -L command line option:
gcc main.o -o main -L/location/of/ncurses -lm -lGL -lGLU -lglut -lncurses
Try installing the ncurses-static package too, if you have only the ncurses-devel package installed in your Ubuntu OS.
If that solves your problem, plus if you add #Joachim's compiling instructions, you are off to a great start.
gcc main.o -o main -L/location/of/ncurses -lm -lGL -lGLU -lglut -lncurses
The linker can't find your shared library in it's search path. If you add the directory where your shared lib is to the LD_LIBRARY_PATH environment variable the linker should find it and be able to link against it. In that case you could omit the -L option to gcc:
gcc main.o -o main -lm -lGL -lGLU -lglut -lncurses
And it should compile fine.
EDIT:
Good to know that apt-get install libncurses5-dev fixes your problem.
FYI.
The LD_LIBRARY_PATH environment variable contains a colon separated list of paths that the linker uses to resolve library dependencies at run time. These paths will be given priority over the standard library paths /lib and /usr/lib. The standard paths will still be searched, but only after the list of paths in LD_LIBRARY_PATH has been exhausted.
The best way to use LD_LIBRARY_PATH is to set it on the command line or script immediately before executing the program. This way you can keep the new LD_LIBRARY_PATH isolated from the rest of your system i.e. local to the current running running instance of shell.
$ export LD_LIBRARY_PATH="/path/to/libncurses/library/directory/:$LD_LIBRARY_PATH"
$ gcc main.o -o main -lm -lGL -lGLU -lglut -lncurses