So I have compiled & linked a program called "embed.exe" using mingw gcc compiler but it I am required by cmd to use "embed.exe" instead of "embed" to run it.
D:\c\embed\bin\release>embed
'embed' is not recognized as an internal or external command,
operable program or batch file.
D:\c\embed\bin\release>embed.exe
Usage: embed [-h] <input>
I want to be able to run it by typing "embed" only. This only happens to my program. Yes, the pathext does contain .exe.
So this has made me think there's something wrong with mingw output, as if cmd doesn't recognize that it's an exe if I don't specify .exe.
Here are all my compiler flags:
-std=gnu11 -march=x86-64 -msse3
-Werror -Wall -Wextra -Wno-unused-parameter -Wno-missing-braces
-Wno-missing-field-initializers -Wpedantic -Wno-format
-flto
-g -D_DEBUG -DDEBUG -Og
-Wl,-subsystem,console
Turns out the problem is not with mingw but with how I create the bin directory.
The problem occurs when I use bash on windows to mkdir the directory, but if I use windows' mkdir, it works. Who would've thought...
How to reproduce the error:
Directory:
embed
|-->main.c
Cd to embed directory.
> gcc -c main.c -o main.o
> bash -c "mkdir bin"
> gcc -o bin/embed.exe main.o -Wl,-subsystem,console
> cd bin
> embed
'embed' is not recognized as an internal or external command,
operable program or batch file.
Cd back and delete bin
> mkdir bin
> gcc -o bin/embed.exe main.o -Wl,-subsystem,console
> cd bin
> embed
Usage: embed [-h] <input>
Read up on PATHEXT . . . it is an optional CMD environment variable that will help you here.
In fact, given a *.EXE file isn't working, I'm guessing you might already have that variable and it doesn't include EXE in the list.
Frank
Related
I am trying to write a script that create a static library call libwork.a in the working directory from all the .c files in the directory:
#!/bin/bash
gcc -c *.c | ar cr libwork.a *.o
But as I run my script, it only creates the object files. The libwork.a does not get created. I tried both sourcing and executing my script but it still only creates object files only.
Why is it not creating the archive?
You are piping the messages printed by gcc (most surely none) to ar (which does not read anything). This is nonesense, ar should run after gcc.
The file listing generated by "*.o" is passed before "gcc" finished.
The solution is to remove that pipe and simply run the commands one after the other.
#!/bin/sh -e
gcc -c *.c
ar cr libwork.a *.o
Note the "-e". This tells the shell to abort if one of the commands fails, so if gcc fails ar will not execute.
Also, have a look at this one:
#!/bin/bash
gcc -Wall -pedantic -Werror -Wextra -c *.c
ar -rc libwork.a *.o
ranlib libwork.a
here's my project tree
project
--bin
--source
----include
------foo.h
----main.c
----foo.c
but when I run gcc -I./source/include ./bin/main.exe ./source/*.c it gave me No such file or directory error.
How can I let gcc know where's the include directory?
You need to run gcc with the -o option to set the output location.
gcc -I./source/include -o ./bin/main.exe ./source/*.c
I'm new to using Makefiles and struggling to build an executable file with file extension of .exe.
Currently, this is what I have which fails to build an executable file:
output: main.o user.o item.o transaction.o bid.o
g++ main.o user.o item.o transaction.o bid.o -o output.exe
main.o: main.cpp
g++ -c main.cpp
user.o: user.cpp user.h
g++ -c -std=c++11 user.cpp
item.o: item.cpp item.h
g++ -c -std=c++11 item.cpp
transaction.o: transaction.cpp transaction.h
g++ -c -std=c++11 transaction.cpp
bid.o: bid.cpp bid.h
g++ -c -std=c++11 bid.cpp
clean:
rm *.o output
I want to make a .exe file because I am creating a batch script to do automated testing of my program. When I create a batch script I try the following in my runTest.bat file:
cd ../../
make
./output current_User_Accounts_File.uaf available_Items_File.ai daily_Transaction_File.tra < Test_Cases/login/login1.in
pause
Problem is I would regularly use bash to execute my program by typing ./ followed by the executable. Using batch file it does not recognize ./ which is why I want to try building .exe file instead. Any help or suggestions is appreciated.
Is there a better strategy to approach this? I have to automate multiple test cases where I take in inputs. When taking the approach of creating .sh scripts instead I run into other conflicts. What are some recommended ways to do automated test scripts for c++?
Here:
g++ main.o user.o item.o transaction.o bid.o -o
the -o option expects the name of the output file, in this case the executable. So:
g++ main.o user.o item.o transaction.o bid.o -o something
or:
g++ main.o user.o item.o transaction.o bid.o -o something.exe
And this is pure GCC/makefile/bash syntax, and nothing particularly to do with the C++ language, so I'm removing that tag.
Windows cmd.exe and most windows programs do not automatigically convert the forward-slash to back-slash. Change ./output ... to output.exe and it should work fine. The .\ would just be redundant anyway.
Suppose there are 2 c program named abc.c and xyz.c . Now we want to work with the 2 executables at a time. So we change the name of the ./a.out using
gcc -g abc.c -o abc
gcc -g xyz.c -o xyz
Even gcc -o abc abc.c works.
What does the -g and -o in the above commands specify or describe?
What is the significance of -g and -o in the command for renaming ./a.out file.
Thanks in advance.
-g means to leave debugging information in the output file, it's unrelated to renaming.
-o means to put the result in the specified file instead of the default filename (abc.o for object files, a.out for linked executable files).
From https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html:
-g
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
-g starts becoming useful once you use debuggers such as gdb and lldb. When you attach to a running program and advancing one line at a time printing/altering the state as it changes.
if we specify -g option while compiling, debugging symbols will be available in the output file which will be useful when you try to debug using GDB.
If we won't specify -o option, the output will be placed in default a.out file. So if we run
gcc a.c - output will be in a.out
gcc b.c - output is a.out which is replacing old a.out file
If you want the output not to be a.out file, you can give -o option while compiling
gcc abc.c -o a
-o and -g options are not related.
I need to compile an old application whose tarball only contains *.c and *h, ie. no Makefile. The root directory contains the application, and a sub-directory contains a library the application needs.
My make/Makefile knowledge isn't great, and I was wondering what the easiest way would be to compile this application + library.
Thank you.
Edit: Using this script...
# cat compile.bash
#!/bin/bash
cd mylib
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
cd ..
gcc *.c -I mylib -L mylib -mylib -o myapp
... I notice that each *.c file in mylib/ is compiled into a *.so file instead of compiling each into an object file and building a single .so file, and I get tons of warnings and errors, eg.
unzip.c: In function âunzipâ:
unzip.c:991: warning: format not a string literal and no format arguments
gcc: unrecognized option '-mylib'
file_util.c: In function âfile_moveâ:
file_util.c:98: error: âerrnoâ undeclared (first use in this function)
I don't know how to compile the library, and then compile the application without error/warning.
No need to use a for loop or generate intermediate object files:
(cd mylib && gcc -shared -fPIC -o libfoo.so *.c) && \
gcc -Imylib -o app *.c mylib/libfoo.so
Compile the library:
cd libfoo
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
After this, you should have a libfoo.so file in libfoo/. Then, compile the program (Don't forget to cd back):
gcc *.c -I libfoo -L libfoo -lfoo -o application
The easiest is probably to get an IDE to do the build for you. Netbeans for one will create a Makefile so you can then build the project independently of the IDE.