Compiling C source files written for POSIX system on Windows - c

To compile a string matching tool SMART in windows command, implemented in C programming language, the instruction was (in the the instruction set, after extracting the zip files):
how to compile smart source code:
makefile is the bash file used for compiling the C source files of the tool. Run the command ./makefile in order to compile the smart
tool. The code of each string matching algorithm is compiled an
tested separately.
The makefile contains the follwoing code:
#!/bin/bash
./logo
echo " Compiling smart.c..................................[OK]"
gcc source/smart.c -O3 -msse2 -o smart -lm
echo " Compiling show.c...................................[OK]"
gcc source/show.c -O3 -msse2 -o show
echo " Compiling selectAlgo.c.............................[OK]"
gcc source/selectAlgo.c -O3 -o select
echo " Compiling test.c...................................[OK]"
gcc source/test.c -O3 -o test
echo " Compiling all string matching algorithms..........."
gcc source/compilesm.c -o compilesm
./compilesm
echo " "
echo " "
It appears to be written to work on a POSIX system, such as GNU/Linux or macOS. I want to compile it on Windows. So, I did the following:
Installed Git-2.37.1-64-bit.exe and opened Git Bash
Executed command: cd C:/Users/DIMITRI/Downloads/smart-13.03
Executed command: ./makefile
But I got the following error:
source/smart.c:44:10: fatal error: sys/ipc.h: No such file or directory
The problem is that I am trying to use the Linux API on Windows. This error will occur for #include <sys/shm.h> also.
Is it possible to overcome this missing header-file problem (these files can not be included in Windows) by using different compiler (I have, x86_64-posix-seh-rev0, Built by MinGW-W64 project, 8.1.0) or installing another system on Windows (without installing Linux or other complicated, heavy system)?
Generally, is there a way to compile C source files written for POSIX system on Windows?
Thanks.
EDIT:
According to one suggestion of one member, I have tried Cygwin, got the same error.

Related

Cmd prompt windows compiling

For my WINAPI projects, I am using atom IDE with c programming, I could compile my code from cmd prompt without a problem until I have started using .rc files. but now that I am using rc files before compiling my program I need to run these commands on the cmd prompt.
gcc -c jake.c
gcc -o jake jake.o -mwindows
windres -o jakerc.o jakerc.rc
gcc -o jake jake.o jakerc.o -mwindows
typing them all again and again to see if my program works correctly each time is really tedious. (Also please do not tell me to use IDEs like DEV c++ or Visual Studio because I don't like them as much as I like atom.)
so I have came up with this solution. I have made an extra file called compile.c which inside it looks like this.
#include <stdio.h>
#include <stdlib.h>
int main() {
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -c jake.c");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -o jake jake.o -mwindows");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul windres -o jakerc.o jakerc.rc");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -o jake jake.o jakerc.o -mwindows");
return 0;
}
When I compile and run this program I get this error:
'C:\\Users\\hashtag\\Desktop\\rawsock\\kokul' is not recognized as an internal or external command,
operable program or batch file.
How do I get rid of this error, and how do I automatically compile my files when I run compile.exe?
as mentioned in my comment, i'd recommend using a batchfile and not a compiled c program for this.
Example:
#ECHO off
SET workingDirectory=%~dp0
SET fileName=%1
echo using %workingDirectory% as working directory
gcc -c %fileName%.c
gcc -o %fileName% %fileName%.o -mwindows
windres -o %fileName%rc.o %fileName%rc.rc
gcc -o %fileName% %fileName%.o %fileName%rc.o -mwindows
you could run your desiered commands with nameofthebatchfile.bat jake
As long as kokul is a directory the command line
C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -c jake.c
Doesn't make sense.
You can combine multiple commands. If you want to change into the directory kokul and start the compile this will work
CD C:\\Users\\hashtag\\Desktop\\rawsock\\kokul & gcc -c jake.c
But anyhow writing a batch file is much easier:
You can do it with two lines
windres -o jakerc.o jakerc.rc
gcc -o jake jake.c jakerc.o -mwindows
Save them to file "mj.bat", then run form command prompt by typing "mj".
When your project grows beyond two or thre files, you should use makefile
GCC and Make Compiling, Linking and Building C/C++ Applications
Check also atom-shell-commands or run-command package for Atom. You can run this directly form editor.
If you're working with command-line tools, this task just seems to cry out for a Makefile. Not only will make automate the necessary actions, it will try to work out from the file timestamps which actions actually need to be carried out. If you haven't changed a C source file, for example, you don't need to recompile it. The effectiveness of make, or similar, will increase enormously as your applications get larger and have more complicated dependencies between components.
You can do some of this stuff with simple batch files but, ideally, the batch file needs to stop when any of the steps encounters an error. There's no point carrying on a long build process that is doomed to fail, and finding the relevant error message in pages of irrelevant output can be a chore. This is another thing that make just does right.
It can take a while to get on top of Makefiles, with their arcane syntax. However, the effort will be rewarded many times over in the longer term.

Nim cross compilation to C

I wrote a Nim program,
echo("Hello.")
And then I tried to cross compile for a Linux machine,
nim c --cpu:i386 --os:linux -c hello.nim
This produced the following output:
config/nim.cfg(45, 2) Hint: added path: '/Users/connor/.babel/pkgs/' [Path]
config/nim.cfg(46, 2) Hint: added path: '/Users/connor/.nimble/pkgs/' [Path]
Hint: used config file '/usr/local/lib/nim-0.10.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: hello [Processing]
Hint: operation successful (8753 lines compiled; 0.140 sec total; 14.148MB)[SuccessX]
At this point I changed into the nimcache/ directory and tried to execute:
gcc hello.c -o hello.o
But that gave me an error:
hello.c:5:10: fatal error: 'nimbase.h' file not found
#include "nimbase.h"
^
1 error generated.
I thought, "no biggie, I'll just find nimbase.h and drop it in the nimcache directory there," but after that I got a new error,
In file included from hello.c:5:
./nimbase.h:385:28: error: 'assert_numbits' declared as an array with a
negative size
...sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
I'm not sure what I'm supposed to do with that. I had tried to use the --genScript option, but that resulted in similar errors. I'm running OS X Yosemite.
Thanks!
Update:
I wasn't sure how many architectures were supported for the --cpu: option, but I found a (partial?) list on the What makes Nim practical blog post. I ended up calling,
nim c --cpu:amd64 --os:linux -c hello.nim
This prevented the error I saw when compiling on my Linux box. If you're using Linux or OS X not sure what your CPU architecture is you can call,
less /proc/cpuinfo
The last problem is because you're running gcc for x86_64 arch, while the sources were generated for i386 arch.
I was having the same issue getting nim to compile executables for Windows, from a GNU/Linux machine, so I made a bash script. It takes the path to the directory containing *.nim source files and the name of the executable file to output.
I'm sure you could swap out the GCC compiler (MinGW in this case) and change the --os: switch as appropriate:
#!/usr/bin/env bash
# Nim must generate C sources only, to be fed to MingW
nim c --cpu:amd64 --os:windows --opt:speed --embedsrc --threads:on --checks:on -c -d:release $1/*.nim
# Copy nimbase.h so MingW32 can find it during compilation and linking
cp /opt/Nim/lib/nimbase.h $1/nimcache/nimbase.h
mkdir -p $1/bin
cd $1/nimcache && x86_64-w64-mingw32-gcc -save-temps $1/nimcache/*.c -o $1/bin/$2.exe
rm $1/nimcache/*.{i,s} # only care about *.o objects
ls -lAhF $1/nimcache
ls -lAhF $1/bin

How to install 32 bit glibc on 64 bit ubuntu

I am trying to learn the C Calling conventions in assembly language. To do so, I made a simple program using the puts function from the C standard library.
I assembled and linked the program with the following commands :-
nasm -f elf file.asm
gcc -m32 file.asm -o file
The nasm produces the right object file but when running the gcc to link the object files, I am getting error.
Looking at the error I have figured it out that I don't have the 32 bit version of glibc on my system. How can I install it. I already have installed this package installed.
I have 64 bit ubuntu 12.04 as my OS.
EDIT :- I have installed the following packages, but the problem is still not solved :-
1)ia32-libs
2) libc6-i386
This command will install the 32bit glibc libraries on 64 bit Ubuntu:
sudo apt-get install gcc-multilib
This is the proper syntax for linking assembly object code into an executable using gcc:
gcc -m32 objectfile.o -o executablefile
(nasm -felf32 already creates objectfile.o; the .asm file should not appear on GCC's command line. GCC can assemble+link a .S file in one step using GAS syntax, but NASM is a separate package.)
I assembled and linked the program with the following commands :-
nasm -f elf file.asm
gcc -m32 file.asm -o file
This is wrong. Your first nasm command is probably creating a file.o file (and you should check that, e.g. with ls -l file.o). The second gcc command does not do what you wish.
But gcc does not know about *.asm file extensions (it knows about .S for preprocessable GNU assembler syntax, and .s for assembler code, but probably handle unknown extensions like .asm as ELF object files by default, however file.asm is not an ELF object file). You should try linking with
gcc -Wall -v -m32 file.o -o file
Notice that you give to GCC an object file in ELF (for the linker invoked by gcc) which you previously produced with nasm.
(you might later remove the -v option to gcc)
Alternatively, use the GNU as assembler syntax (not the nasm one), name your file file.S (if you want it to be preprocessed) or file.s (without preprocessing) and use gcc -v -Wall -m32 file.s -o myprog to compile it.
BTW, to understand more about calling conventions, read the x86-64 ABI spec (and the similar one for 32 bits x86 ...), make a small C example file some-example.c, then run gcc -S -fverbose-asm -O some-example.c and look into the produced some-example.s with an editor or pager.
Learn also more about ELF then use readelf (& objdump) appropriately.
You want to install a package called 'ia32-libs'

Change build output directory when building via terminal

Recently, I found a program that is kind of a mix between an IDE and a text editor. It supports the syntax of the language and it does formatting, but it does not build and run the program for you. I am running Mac OS X 10.6.8. I looked up how to build C code using the Terminal application. The format is:
gcc [file]
Pretty simple. The problem is that I cannot change the directory of where the built file is outputted, nor can I change the name. By default, every file compiled is outputted in the home directory by the name of 'a.out.' How can I specify the output directory and name?
Thanks!
gcc has a -o option to change the output name. You can specify the path there. E.g.:
$ ls
program.c
$ gcc program.c -o program
$ ls
program program.c
$ mkdir bin
$ gcc program.c -o bin/program
$ ls bin
program
$
You should probably also want to know about a few other common options:
-std=c99, -std=gnu99: Use the c99 standard / with gnu extensions.
-Wall, -Wextra, -pedantic: Enable extra warnings.
-O0 -ggdb: Compile with debugging symbols. Look up how to use gdb.
-O2: Compile with processor-independent optimizations. Not compatible with -O0.

How to compile a program of c Language manually on MS DOS instead of Borland

I need to compile a program in MS DOS. I have Borland Editor, I can compile it using Alt+F9 but the things is what it do at the backend. I want to compile it in MS DOS. I m trying this:
c:\tc\bin>tcc -o hello.exe hello.c
where hello.c is is my file, hello.exe the file I want to produce. Its not working, what shouldI do? and also please tell me also how do I compile .cpp file manually from MS DOS.
If I remember correctly, Borland/Turbo C compiler's command line options didn't look like gcc options. You should try tcc /? for a command line help.
Turbo C++ Version 3.00 Copyright (c) 1992 Borland International
Syntax is: TCC [ options ] file[s] * = default; -x- = turn switch x off
-1 80186/286 Instructions -2 80286 Protected Mode Inst.
-Ax Disable extensions -B Compile via assembly
-C Allow nested comments -Dxxx Define macro
-Exxx Alternate Assembler name -G Generate for speed
-Ixxx Include files directory -K Default char is unsigned
-Lxxx Libraries directory -M Generate link map
-N Check stack overflow -O Optimize jumps
-P Force C++ compile -Qxxx Memory usage control
-S Produce assembly output -Txxx Set assembler option
-Uxxx Undefine macro -Vx Virtual table control
-X Suppress autodep. output -Yx Overlay control
-Z Suppress register reloads -a Generate word alignment
-b * Treat enums as integers -c Compile only
-d Merge duplicate strings -exxx Executable file name
-fxx Floating point options -gN Stop after N warnings
-iN Max. identifier length -jN Stop after N errors
-k Standard stack frame -lx Set linker option
-mx Set Memory Model -nxxx Output file directory
-oxxx Object file name -p Pascal calls
-r * Register variables -u * Underscores on externs
-v Source level debugging -wxxx Warning control
-y Produce line number info -zxxx Set segment names
C:\TC\BIN>
So, I think you should type:
tcc hello.c for C programs and tcc -P hello.cpp for C++ programs.
I belive this things must work
c:\tc\bin\tcc -c File.c \\ To generate objective file
c:\tc\bin\tcc -o File.obj \\ To generate exe from obj and please use .obj and not .o
c:\tc\bin\ tcc -run File.c \\ to generate exe file without .obj file
c:\tc\bin\File.exe \\ to run the exe file
I dont know why the
tcc -o good.exe File.obj \\not working, the error is good.exe file not found
I think we cant give a name to .exe file in tcc command line prompt.but its possible in gcc. I dont know about TCC much. If i find it i will let you know it !
Just take a look at these http://bellard.org/tcc/tcc-doc.html#SEC3. This is what I found on google . and googling makes you more powerful so keep on googling the things when you dont know .
Thanks
Further to Prof Falken's answer
tcc file.c <-- will compile in C
tcc file.cpp <-- will compile in cpp
tcc file.ext where .ext is anything other than cpp, will compile in C Unless --P is used then cpp is used to compile it, in which case .cpp is used, even if the extension is .c
I am running TCC in a VM and can't copy/paste from there here. But your test should find the same result as mine, if not, then perhaps I erred, but you can test for yourself given this code that works in C and not CPP, and code that works in CPP and not C. You can then experiment with changing the extension, and using -P or not.
The following code works in C only
conly.c
(A C++ expert told me re the following example, works in C and not C++, because C allows void* -> T* conversions. C++ does not)
#include <stdio.h>
#include <stdlib.h>
void main() {int *x=malloc(4);}
The following code works in C++ only
cpponly.cpp
#include <stdio.h>
void main() {
int a=9;
int& b=a;
printf("b=%d",b);
}

Resources