Linking with gcc on Windows: - c

I'm trying to link a c-file (cvode.c) towards a set of .dll files. I run the following two lines in a cmd:
>>> gcc -c -I C:\sundials\inst2\include cvode.c
>>> gcc -shared -o cvode_lib.dll cvode.o -L. C:\sundials\inst2\lib\sundials_cvode.dll -L. C:\sundials\inst2\lib\sundials_nvecserial.dll
The first command does not give any errors but the second one gives the following error message:
C:\sundials\inst2\lib\sundials_cvode.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
If I try to import the dll-files with ipython it works perfect, and the same two lines (with the same source files) works perfectly on my second computer running the same OS (windows 10 64bit) with the same version of gcc (installed with the latest Mingw64-installer). I've also tried to install cygwin64 and compile with its gcc-version, without success. The file compiles perfectly fine on an Ubuntu computer as well.
I have also tried to link vs the corresponding <sundials>.lib-files but that is not working either.
Googling for solutions has not been successful, maybe because I am not too familiar with c or gcc. However since it runs perfect on my other Win10 PC I fear it might be something deeper...

Related

Cygwin Shell: $ make ex1 returns: "The system cannot find the specified file" on Windows 7

I'm following the steps of the online book: "Learn C The Hard Way", and since I'm using Windows 7, I've installed Cygwin to use the Linux commands. But I'm facing a problem just on the first exercise of the book. I'm supposed to put the following command on the shell:
$ make ex1
After creating a ex1.c file on the folder. The command should give me:
cc ex1.c -o ex1
But instead, I'm getting the following message:
$ make ex1
cc ex1.c -o ex1
process_begin: CreateProcess(NULL, cc ex1.c -o ex1, ...) failed.
make (e=2): The system cannot find the specified file.
make: *** [ex1] Error 2
What's wrong?
First of all you should know that you should be running the command in the same directory where the file is. In cygwin, first you will have to locate to the folder in which the file is present, then you can run these make commands. Better since you are using Windows. You should better use any other windows based client for C. But if I were at your place. I would have installed a virtual Linux environment on my local windows computer and would have worked on that. You should try that once. Linux Terminal gives a lot of power to the developer. There are a lot of things which you can do on a terminal which is not supported by cygwin. For compiling C programs on Cygwin, I believe you should check if it supports compiler commands or not. :)
Make is reporting that it can not find cc.
cc is a link to gcc, and it belongs to gcc-core.
$ cygcheck -f /usr/bin/cc
gcc-core-5.4.0-1
To verify if the package is correctly installed
$ cygcheck -c gcc-core
Cygwin Package Information
Package Version Status
gcc-core 5.4.0-1 OK
If, as likely, the package is missing, you need to install it with the cygwin setup.

makefile works with cygwin on windows but not ubuntu

I have the following folder on my git repo - https://github.com/ryu577/base/tree/master/numerical/c/NumericalRecipiesCode/lib
On my windows machine which has cygwin installed, I can run make in that directory, which triggers the command -
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I ../include -lm
This puts the tst_libfns.o and fileio.o in the obj directory.
Now, I pulled this repository into my ubuntu machine and tried the same thing. However, when the same command is generated there, it gives me the following error:
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I../include -lm
../obj/fileio.o:fileio.c:(.text+0x52): undefined reference to `__getreent'
../obj/tst_libfns.o:tst_libfns.c:(.text+0x10): undefined reference to `__main'
collect2: error: ld returned 1 exit status
make: *** [../bin/lib/tst_libfns] Error 1
Am I missing something obvious?
I just figured it out. I had pulled in the .o files through git in the obj directory and gcc was somehow trying to use the existing files (which had been generated in Cygwin + Windows). Somehow, those .o files don't seem to be compatible with linux. When I delete them and run the make command again, the .o files are re-generated. I guess the moral of the story here is that binaries and executables generated by GCC in Windows are incompatible with Linux (and I'll guess vice versa).

Using GCC to compile C code

I installed MinGW on my Windows 8 laptop and tried to compile a C code file with
gcc test.c -o test.exe
the compiler gave no warnings or errors but it did not create test.exe
how do i get the compiler to create the file
test.c
My terminal session
An interesting observation:
When I deliberately introduce an error in the code and try to compile the compiler shows the error
Code with error
Compiler output
When I try compiling the same code using Command Prompt
This is what it shows
But the file does exist in the MinGW\bin directory
I moved the
test.c
file to
C:\
and started the command prompt in the
C:\MinGW\bin
directory
and here is what it outputs
Problem partially solved:
I disabled hybrid boot in windows 8 and restarted the computer. The compiler now works in Command Prompt but not in PowerShell.
Try to compile your code normally as
gcc test.c
If you get default output file a.exe,then go for
gcc test.c -o test.exe
I would suggest you go through this compilation instruction :-
gcc -o test.exe test.c
I believe this code runs perfectly on your windows system.Please inform if it doesn't!
I know this is an old question, but I came across this after having this same issue and managed to solve it.
When I installed MinGW on my computer, I didn't add the MinGW bin directory to my PATH (<mingw install dir>\bin). I had written some code that referred to the GNU compiler binaries by their full path, and when I tried to compile something I experienced the same behavior you described.
So it seems like MinGW won't work properly unless it is added to your PATH. I think it's weird that gcc didn't complain about it though.
One possibility is Microsoft's use of VirtualStore.
This can cause problems with "missing" files with Cygwin. See for example, Cygwin sees a file that windows can't--I want to access this file from python and https://superuser.com/questions/400600/file-only-visible-to-cygwin-not-windows.
To verify whether this is the case, try doing a search of your entire hard drive for the file test.exe. Or try MinGW's ls rather than dir.
And since the OP "partially solved" the problem by moving to another directory, this could be the cause.

gcc library linking issue in Windows after cygwin installation

C:\slite\1.1>gcc -c ".\src\SDR.c" -o ".\obj\SDR.o" -I".\inc"
C:\slite\1.1>gcc ".\obj\SDR.o" -o ".\exe\SDR.exe" -L".\lib" -lsres -lshis
.\lib/sres.dll: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Could not compile C program using gcc after installing cygwin in Windows 2003.
Before installing cygwin, I was able to compile successfully.
I am compiling the code from Command prompt, not from cygwin terminal.
The gcc being used is from Dev-Cpp distribution.
Is the problem because of the directory separator ('/') between the libraries directory specified by -L and individual libs specified by -l, being used by gcc after cygwin was installed?
If i try to compile in cygwin terminal I am getting unknown function calls error, though the functions are defined in the headers included.
EDIT:
C:\srmlite\x7.5.0.146-1600>where gcc
C:\Dev-Cpp\bin\gcc.exe
The gcc was not from cygwin.
I have uninstalled cygwin and removed its directory too. I still see this problem.
Try to add the correct bin directory into the path in your Environment variables.
Something like this: C:\soft\cygwin\bin
Seems that Cygwin modified the default environment, adding itself to %PATH%, so you're actually calling cygwin's gcc, check that out.

How can I link against glut32 in cygwin?

I am trying to run someone else's (4 year old) code from sourceforge. I downloaded cygwin and checked out the project with CVS.
Here is the compile line which is failing:
gcc ../block_display/block_display.c -o block_display -lopengl32 -lglut32 -lm
Here is the relevant include statement in block_display.c:
#include <GL/glut.h>
When I try to run the above compile line, I get this compile error:
$ gcc ../block_display/block_display.c -o block_display -lopengl32 -lglut32 -lm
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lglut32
collect2: ld returned 1 exit status
I admit I am new/rusty with cygwin. I tried a few things to get to this point, but since I don't entirely know where to put files, I am stuck on this error. Here is what I have tried:
Downloading all results for 'glut' in cygwin setup: libglut-devel, freeglut, libglut3
Downloading glut 3.7.6, copying glut32.dll to C:\Windows\SysWOW64, and copying glut.h to C:\cygwin\usr\include. I still have glut.def and glut32.lib sitting around, but I do not know exactly where to place them. I tried following this install guide, but since I am not running VC++, I do not know what the cygwin equivalent of VC++ path is.
Any idea what I could do to get this code to compile successfully? I am running Windows 7 64-bit.
You should use freeglut instead. The original glut is far outdated. Freeglut on the other hand is binary and source compatible with the original glut, and it's also open source.
Since glut32.dll is a windows DLL and cygwin is for emulating a unix environment on windows, you can't easily use the original glut with cygwin without recompiling from source.

Resources