mingw to cygwin - c

I am a newbie in cygwin. However, I have used mingw so far, but it is not supporting fork(), so I need to switch to cygwin. I have created a build.bat file in my mingw (programming language C):
gcc -o mask mask.c -pg -I/c/opencv/build/include -lopencv_core231 -lopencv_highgui231 -lopencv_imgproc231 -L. -L/c/opencv/build/x86/mingw/lib
Can anyone suggest me how I can run this .bat file at cygwin or refer me to a site.
How can I run mingw executable with cygwin? Any refernce will be appreciated.
Thanks for your help
Emon

I believe that cygwin can run cmd (DOS command interpreter), which can then run your batch file. The problem is that cmd won't be able to use the non-windows paths that you have in your batch script. Try to run: cmd /c myBuildFile.bat at a cygwin prompt, and you should see what I mean.
It is better to use cygwin-style tools for making a build script. You should have the make program on your path which will allow you to write simple and more complex build scripts.
Alternatively you could convert your .bat build script into a shell script, something like this:
#!/bin/sh
gcc -o mask mask.c -pg -I/c/opencv/build/include -lopencv_core231 -lopencv_highgui231 -lopencv_imgproc231 -L. -L/c/opencv/build/x86/mingw/lib
Name it anything, e.g. myBuildScript.sh, then make it executable with: chmod u+x myBuildScript.sh.

Related

Is there such a thing as a MSYS script file?

I'm running MSYS under Windows and although it works I want to know if there is such a thing as a MSYS script file.
For example to compile a GTK program in C I need to type: gcc source.c -o executable.exe 'pkg-config --cflags --libs gtk+-3.0' to compile. Is there a way to store this in a file and run the file from MSYS without having to type in the command each time (IE like .bat, .sh files)?
If you want to automate building an application, just use a build system. The Meson build system is what the GTK+ developers recommend now (and GStreamer, and many other projects). It's multiplatform, and available as a package in MSYS2 (I used it there, works like a charm). They have a simple Meson tutorial where they build a GTK application. That's 3 lines of code.

How to execute and compile a program in Xcode via command line in c and where's the executable?

I'm trying to execute via command line a code written in C. I tried gcc -o file file.c, but it did not work. I need to learn how to compile and execute a code using gcc and llvm without graphical interface. Furthermore when I compile the program I cannot find the executable file in Finder (there's no Developer folder in Library).
Thanks in advance.
You can use xcrun tool:
#/usr/bin/xcrun cc -o file file.c
Note: if you have several Xcode versions you can chose with xcode-select and your command above will use compiler and the rest of the tools from the selected SDK.
If file.c is in the Desktop directory: Did you change to that directory beforehand?
Usually Terminal.app starts in the home directory, e.g.: /Users/yourname
To get to the Desktop directory:
cd ~/Desktop
Then check if the source file is there:
ls -l file.c
Then try again to compile:
gcc -o file file.c
Check for any error messages. If no output is given everything is fine and there should be an executable which can be (surprise!) executed:
ls -l file
./file

How can I use gcc by installing Cygwin on a Windows machine?

i have installed the cygwin package for my netbeans IDE. I can use that in netBeans project. But now I want to use the gcc compiler from a cmd prompt. How can I do that?
In Linux we open a terminal and type
gcc filename.c
and it compiles. Now I want to do the same thing in Windows with Cygwin's gcc. Can I type gcc filename.c in cmd and it compiles? If so, how?
Edit :
by writing in cmd
gcc --version
I get Access is denied
Edit 1:
In the C: drive I have a folder named Cygwin that contains Cygwin.bat.
When I run that, a new prompt is opened and inside that when I type gcc filename.c, it works.
In that .bat file :
#echo off
C:
chdir C:\cygwin\bin
bash --login -i
I don't understand what that means.
I see from your latest comment that it works when you run gcc from the Cygwin bash shell.
The Access is denied message was appearing when you tried to run gcc from the Windows cmd prompt. I don't know why you'd get that particular message.
My advice is just to use the bash shell. (It also has a lot of nice features that the Windows command shell lacks.) If that's a good solution for you, feel free to stop reading now.
But if you really want to use Cygwin tools (such as gcc) from a Windows prompt, you need to update your Windows %PATH% to include the Cygwin bin directory. As seen from bash, the directory is /usr/bin/; from Windows, it's going to be something like C:\cygwin\bin (assuming you installed Cygwin in C:\cygwin, which is the default).
To permanently add C:\cygwin\bin to your Windows %PATH%, open System Properties in the Control Panel, tap the "Environment variables" button, and adjust the value of Path in "System variables". Once you've done that, newly opened cmd windows should have the new %PATH% setting. (The user interface for modifying environment variables isn't exactly use-friendly; maybe somebody else can suggest a better way.)
EDIT:
The cygwin.bat batch file changes the current director to C:\cygwin\bin and then launches the Cywgwin bash shell in a new window. That gives you an environment in which gcc works by default, since your $PATH is already set up correctly. The windows command shell and the Cygwin bash shell are quite different environments.
If you installed the gcc package for cygwin, you should be able to do simply gcc filename.c.
The setup.exe file of Cygwin lets you choose any additional package you want to add when installing. If you skipped it, simply rerun the setup.exe.
Packages can be also found on the Cygwin website.
does your cmd recognizes gcc as an installed application?
if so, try this
gcc -c filename.c -o filename.o
gcc is actually a link (which the Command Prompt does not understand). Use gcc-3 or gcc-4 if you need to use the Command Prompt.

Makefile in Windows

I have a folder named 'X'. In X I have two subfolders: src and include.
The src folder contains a C file main.c, and the include folder contains a file main.h.
I want to write a makefile to build these files(from folder X) in windows command prompt.
I have tried a lot but with no effect.
First of all I need the make file format in Linux for this, and once I have that i will do that in Windows.
Can anybody help me in doing this?
Try the following links, in there you'll find the format, and basically all that you need to build your Makefile:
http://mrbook.org/blog/tutorials/make/
http://www.opussoftware.com/tutorial/TutMakefile.htm
http://oucsace.cs.ohiou.edu/~bhumphre/makefile.html
Once you've made your Makefile, you can use MinGW's mingw32-make - A Windows native build of GNU make.
Open the folder (X) in file explorer.
Right click anywhere > new > text document.
Right click on the file > rename > remove .txt extension.
Open up the makefile with your favorite text editor.
You can use NMake, which is the windows alternative do Linux make. It is a command-line tool included with Visual Studio.
Although the syntax is very similar, you will find some differences. So the makefile that works on Linux may not work directly with NMake in windows.
In Linux, these lines for example:
gcc -c main.cpp -o main.o
gcc -o app main.o -lstdc++
In Windows, probably should be changed to:
cl.exe /c main.cpp /Fomain.o
link.exe /OUT:app.EXE main.o
So the makefile require edition to work with NMAKE in windows.
To use the makefile "as is", you should try a tool to mimic Linux commands in Windows, like Cygwin.

Using Cygwin to Compile a C program; Execution error

I'm enrolled in a masters computer science course. The course is using C and the instructor wants us to use Cygwin to compile programs if we are using windows.
I've downloaded and installed Cygwin and I've ensured that I've installed the GCC compiler.
But I don't know where to go from here. I need to compile a single source file that has a basic include.
#include <stdio.h>
Lets assume the file is on my desktop (it is not, but for the sake of argument). How do I navigate to the desktop from the bash shell? I assume once I've navigated to the correct location in bash, I simply execute:
gcc myProgram.c -o myProgram
Update: Following different instructions posted below, I was able to compile the program; I thank you for that. But when I execute the resulting binary I get the following. How can I compile or execute this program so I don't get the error? Again, thank you.
This application has failed to start because cygwin1.dll was not found. Re-installing the application may fix this problem.
when you start in cygwin, you are in your $HOME, like in unix generally, which maps to c:/cygwin/home/$YOURNAME by default. So you could put everything there.
You can also access the c: drive from cygwin through /cygdrive/c/ (e.g. /cygdrive/c/Documents anb Settings/yourname/Desktop).
Regarding your updated question about the missing cygwin1.dll.
From the Cygwin terminal check,
ls /usr/bin/cygwin1.dll
If it is not present (I doubt that), your installation is not properly done.
Then, check your path with,
echo $PATH
This will give : separated list of paths. It MUST contain /usr/bin. If you find that missing add it with,
export PATH=/usr/bin:$PATH
Finally,
I hope you are using Cygwin from the cygwin terminal (the little green+black icon installed with Cygwin), or MinTTY (if you installed that).
And, you have not moved the compiled EXE to a different machine which does not have Cygwin installed (if you do that, you will need to carry the cygwin1.dll to that machine -- keep it in the same folder as the compiled EXE).
Just to summarize, here are some commands that navigate to a directory and compile code using Cygwin and Windows Vista:
Start a Cygwin shell.
At the prompt, use cd to change to the appropriate directory:
$ cd /cygdrive/c/Users/nate/Desktop
Use ls to list the files in the directory:
$ ls
prog.c
Use the gcc command to compile a file in this directory:
$ gcc prog.c -o prog
If you don't see any errors, you should be able to run the resulting program:
$ ./prog
Update:
For the "Cygwin1.dll not found" error, I like Nik's answer. You might also check out this related post about cygwin1.dll not found, which suggests adding c:\cygwin\bin\ to your Windows PATH.
There are instructions on how to change the Windows PATH variable for Windows XP, and on Vista I think it's similar.
Go to Control Panel -> System
Select Advanced System Settings
Click on the Advanced tab
Click on Environment Variables
Under System Variables, find the Path entry and click Edit
Add c:\cygwin\bin to the list, making sure to separate it from any previous items with a semicolon
Look for (that is, cd to)
/cygdrive/c/
that will usually be your C:\
Also look at Using Cygwin, the Lifehacker introduction (June/2006) and, this biomed page at PhysioNet.
Regarding the cygwin1.dll not found error, a solution I have used for at least 8 years is to add the Cygwin bin directories to the end of my %PATH% in My Computer -> Properties -> Advanced -> Environment Variables. I add them to the end of the path so in my normal work, they are searched last, minimizing the possibility of conflicts (in fact, I have had no problems with conflicts in all this time).
When you invoke the Cygwin Bash Shell, those directories get prepended to the %PATH% so everything works as intended in that environment as well.
When not running in Cygwin shell, my %PATH% is:
Path=c:\opt\perl\bin; \
...
C:\opt\cygwin\bin; \
C:\opt\cygwin\usr\bin; \
C:\opt\cygwin\usr\local\bin;
This way, for example, ActiveState Perl's perl is found first when I am not in a Cygwin Shell, but the Cygwin perl is found when I am working in the Cygwin Shell.
If you are not comfortable with bash, you can continue to work in a standard windows command (i.e. DOS) shell.
For this to work you must add C:\cygwin\bin (or your local alternative) to the Windows PATH variable.
With this done, you may:
1) Open a command (DOS) shell
2) Change the directory to the location of your code (c:, then cd path\to\file)
3) gcc myProgram.c -o myProgram
As mentioned in nik's response, the "Using Cygwin" documentation is a great place to learn more.
If you just do gcc program.c -o program -mno-cygwin it will compile just fine and you won't need to add cygwin1.dll to your path and you can just go ahead and distribute your executable to a computer which doesn't have cygwin installed and it will still run. Hope this helps
Windows path C:\src under cygwin becomes /cygdrive/c/src
Compiling your C program using Cygwin
We will be using the gcc compiler on Cygwin to compile programs.
1) Launch Cygwin
2) Change to the directory you created for this class by typing
cd c:/windows/desktop
3) Compile the program by typing
gcc myProgram.c -o myProgram
the command gcc invokes the gcc compiler to compile your C program.
You might be better off editing a file inside of cygwin shell. Normally it has default user directory when you start it up. You can edit a file from the shell doing something like "vi somefile.c" or "emacs somefile.c". That's assuming vi or emacs are installed in cygwin.
If you want to file on your desktop, you'll have to go to a path similar (on XP) to "/cygwindrive/c/Documents\ and\ Settings/Frank/Desktop" (If memory serves correctly). Just cd to that path, and run your command on the file.
Cygwin is very cool! You can compile programs from other systems (Linux, for example), and they will work. I'm talking communications programs, or web servers, even.
Here is one trick. If you are looking at your file in the Windows File Explorer, you can type "cd " in your bash windows, then drag from explorer's address bar into the cygwin window, and the full path will be copied! This works in the Windows command shell as well, by the way.
Also: While "cd /cygdrive/c" is the formal path, it will also accept "cd c:" as a shortcut. You may need to do this before you drag in the rest of the path.
The stdio.h file should be found automatically, as it would be on a conventional system.
This file (cygwin1.dll) is cygwin dependency similar to qt dependency.you must copy this file and similar files that appear in such messages error, from "cygwin/bin" to folder of the your program .Also this is necessary to run in another computer that have NOT cygwin!

Resources