Using system() cross platform. Compiling with cygwin and g++ - c++

I'm using cygwin and g++ under Windows 7 to compile my project. I've made a makefile to compile the project under Ubuntu. This is all working so far.
A function calls the Libre Office to convert some files. For this purpose i pass a command string to system().
Symptom
When I use the executable generated with cygwin a error occurs:
sh: C:\..\LibreOffice 3.6\program\soffice --headless --convert-to png:'draw_png_Export' add1.fodg : command not found
What irritates me is the fact, that it seems the command is passed to sh and not cmd
How can I make sure the executable build for Windows uses no sh?

I think you have two options.
First, stick with sh used by system() and use the path to LibreOffice in POSIX format.
C:\..\LibreOffice 3.6\program\soffice
will become
/cygdrive/c/../LibreOffice 3.6/program/soffice
You can also use the cygpath utility for this, as well.
The other option is to call LibreOffice via cmd:
cmd /c C:\..\LibreOffice 3.6\program\soffice args...

Related

How to fix 'sh: brew: command not found' in Xcode?

I'm making a command-line tool in Xcode, and want to call terminal commands with my C++ code.
I found the system() function can run terminal commands. And I know that the commands I want work in my Terminal. But these commands are not working the same way when I call them with system() in Xcode.
For example, I tried calling the brew command in Xcode.
system("brew");
I expect the output to be the same as when I type brew into my terminal:
Example usage:
brew search [TEXT|/REGEX/]
...etc
but the actual output in Xcode is sh: brew: command not found.
Why is system() acting differently than terminal? Should I be using a different function?
system does not load your shell's configuration files, and that's where /usr/local/bin is added to the PATH environment variable.
(That directory is not included by default on OS X.)
The simplest solutions are probably to either use the full path, or start XCode from the terminal so it inherits your shell's PATH.
(More details about how PATH works, and environment variables in general, can be found in any introduction to Unix-y systems.)

Error relating another make utility in one system

I have google how to install a gnu make file. And I follow the following steps:
$ ./configure
$ sh ./build.sh
$ ./make check
Unfortunately when I run the second command, my cygwin shell complains:
In file included from c:\qt\tools\mingw48_32\i686-w64-mingw32\include\sys\stat.h :14:0,
from ./makeint.h:71,
from ./ar.c:18:
./makeint.h:525:10: error: conflicting types for 'lseek64'
long int lseek ();
I am aware there is another make utility under the qt mingw, but how can i still proceed to install the new make?
The source distribution of GNU make from the FSF download site is not technically supported on Cygwin. Not that there's any known reason why it shouldn't work, but it's not tested and last I heard the Cygwin guys were maintaining some extra patches to GNU make. So it doesn't surprise me that there are issues.
Remember that you should only use the Cygwin version of GNU make if you're invoking make inside of Cygwin to build Cygwin applications. You should not use the Cygwin version of GNU make to build native Windows content outside of Cygwin. Do not mix them together.
You can build a native version of GNU make for Windows (not cygwin) by following the directions in the README.W32 file that comes with the source code. You can get binary versions by looking for MSYS / MinGW.
If you need a Cygwin version of GNU make, you should get ahold of the source code for GNU make from the Cygwin distribution and use that. If it fails to work, you need to communicate with the Cygwin folks about it.

Building a Windows Console App In Cygwin 1.7.16 Targeted to Cygwin 1.5.24

I have a Windows console app that is pure standard C++. I am building it in Cygwin 1.7.16 (running on Windows 7) for target Cygwin 1.5.24 (running on Windows XP SP 2).
My build command is:
g++ -o mgen_stats.exe -I ../include ../src/*.cpp
On the target, my path includes /usr/bin, which is where cygwin1.dll lives.
When I run the application with no arguments on the target in a Cygwin 1.5.24 shell, I immediately get the command prompt back. I should see the application's usage printed out.
Some immutable constraints I'm working under:
The build and target machines are isolated from each other.
There is no C++ build tool chain on the target.
I may not install a C++ build tool chain on the target.
I may not modify the C++ build tool chain I have on the build machine.
As a result of the above, I must build on my build machine, burn the .exe to a CD, and copy it onto the target machine.
I would be just as happy for my application to be a native Windows application (i.e. run it in a DOS shell rather than a Cygwin 1.5.24 shell). To this end, I tried using g++ build flag -mno-cygwin. g++ tells me that flag -mno-cygwin has been removed. It then suggests that I "use a mingw-targeted cross-compiler". I do not know how to tell if I have the mingw-gcc, mingw64-i686-gcc, and mingw64-x86_64-gcc packages (which, according to the Cygwin FAQ, contain the suggested cross-compilers), and if I do have them, I do not know what the names of the compiler executables are.
Can anybody help me get this app built such that it will run successfully under either a DOS or Cygwin 1.5.24 shell on my Windows XP SP 2 target?
Thank you in advance.
The MinGW packages (like any other Cygwin package) can be installed on Cygwin using the "setup.exe" that Cygwin provides on its website. If you do have them installed, you'll find the compilers as i686-w64-mingw32-g++ or x86_64-w64-mingw32-g++.
Alternatively, you can also install the MinGW packages directly (without using Cygwin).

Run C++ in command prompt - Windows

I know that everyone uses an IDE nowadays, but I just find it simpler to write my code in notepad++, compile it using a command prompt command, and run it from there too. At least that works for Java and Python. I've tried to get my head around how to do that with C++, and haven't been able to find anything good. Is there any compiler (like Java's JDK) that I can stick into my path and use the C++ equivalent of javac and java to run and compile my code from CMD?
Note: please don't post answers and comments about how IDEs are better - I know they are. I'm just used to doing it the old way :D
Steps to perform the task:
First, download and install the compiler.
Then, type the C/C++ program and save it.
Then, open the command line and change directory to the particular one where the source file is stored, using cd like so:
cd C:\Documents and Settings\...
Then, to compile, type in the command prompt:
gcc sourcefile_name.c -o outputfile.exe
Finally, to run the code, type:
outputfile.exe
If you're running Windows then make use of this:
g++ -o program program.cpp
g++ is the name of the compiler and -o is the option needed for creating a .o file. Program (without .cpp suffix) is the exe file and program.cpp is your source file that you want to compile.
g++ -o program program.cpp&program.exe
Use this shortcut to run the .exe file of the program. This might run in Linux but you may have to use .out suffix instead of .exe. Use this handy batch script to execute your programs on Windows:
#echo off&&cls
set /p pathName=Enter The Path where the file is located:%=%
cd %pathName%
REM set /p exec=Enter The Name of the executable you want to make:%=%
set /p file=Enter The Name of the file you want to compile:%=%
g++ -o %file% %file%.cpp
%file%.exe
save it as cppExecutor.bat
Also you could use the following commands on Unix (Linux and Mac) OS:
CC program.cc
If you want to use gcc:
gcc -o program program.cpp
With the shortcut:
gcc -o program program.cpp&program.exe
It depends on what compiler you're using.
For example, if you are using Visual C++ .NET 2010 Express, run Visual C++ 2010 Express Command Prompt from the start menu, and you can simply compile and run the code.
> cl /EHsc mycode.cpp
> mycode.exe
or from the regular command line, you can run vcvars32.bat first to set up the environment. Alternatively search for setvcvars.cmd (part of a FLOSS project) and use that to even locate the installed VS and have it call vcvars32.bat for you.
Please check your compiler's manual for command lines.
Sure, it's how most compilers got started. GCC is probably the most popular (comes with most flavors of *nix). Syntax is just gcc my_source_code.cpp, or gcc -o my_executable.exe my_source_code.cpp. It gets more complicated, of course, when you have multiple source files (as in implementation; anything #included works automatically as long as GCC can find it).
MinGW appears to be a version of GCC for Windows, if that's what you're using. I haven't tried it though.
Pretty sure most IDEs also include a command line interface. I know Visual Studio does, though I have never used it.
I really don't see what your problem is, the question is rather unspecific. Given Notepad++ I assume you use Windows.
You have so many options here, from the MinGW (using the GCC tool chain and GNU make) to using a modern MSVC. You can use the WDK (ddkbuild.bat/.cmd or plain build.exe), the Windows SDK (nmake.exe), other tools such as premake and CMake, or msbuild that comes with MSVC and the Windows SDK.
I mean the compiler names will differ, cl.exe for MSVC and the WDK and Windows SDK, gcc.exe for MinGW, but even from the console it is customary to organize your project in some way. This is what make and friends were invented for after all.
So to know the command line switches of your particular compiler consult the manual of that very compiler. To find ways to automate your build (i.e. the ability to run a simple command instead of a complex command line), you could sift through the list on Wikipedia or pick one of the tools I mentioned above and go with that.
Side-note: it isn't necessary to ask people not to mention IDEs. Most professional developers have automated their builds to run from a command line and not from within the IDE (as during the development cycle for example), because there are so many advantages to that approach.
Download MinGW form : https://sourceforge.net/projects/mingw-w64/
use notepad++ to write the C++ source code.
using command line change the directory/folder where the source code is saved(using notepad++)
compile: g++ file_name.cpp -o file_name.exe
run the executable: file_name.exe
first Command is :
g++ -o program file_name.cpp
Second command is :
.\program.exe
Let us Check this image
A better alternative to MinGW is bash for powershell. You can install bash for Windows 10 using the steps given here
After you've installed bash, all you've got to do is run the bash command on your terminal.
PS F:\cpp> bash
user#HP:/mnt/f/cpp$ g++ program.cpp -o program
user#HP:/mnt/f/cpp$ ./program
This is what I used on MAC.
Use your preferred compiler.
Compile with gcc.
gcc -lstdc++ filename.cpp -o outputName
Or Compile with clang.
clang++ filename.cpp -o outputName
After done compiling. You can run it with.
./outputFile
Open cmd and go In Directory where file is saved. Then,
For compile,
g++ FileName. cpp
Or
gcc FileName. cpp
For Run,
FileName. exe
This Is For Compile & Run Program.
Make sure, gcc compiler installed in PC or Laptop.
And also path variable must be set.
have MinGW compiler bin directory added to path.
use mingw32-g++ -s -c source_file_name.cpp -o output_file_name.o to compile
then mingw32-g++ -o executable_file_name.exe output_file_name.o to build exe
finally, you run with executable_file_name.exe
[Working 100%] from a Windows user.
Open the terminal(powershell) where your file.cpp is created.
g++ file.cpp //it will compile the file into a.exe
.\a.exe //this will run the program.
There are few ways:
Using GNU Compiler Collection (GCC):
gcc -lstdc++ filename.cpp -o outputName
Using g++ command:
g++ -o outputName filename.cpp
Using clang++:
clang++ filename.cpp -o outputName
You can run your code by just typing
To Compile
g++ file_name.cpp
To Run:
a
only this you have to do to run c++ code in cmd which is written in notepad++
enter image description here
enter image description here

g++ not working on Windows command prompt. Cygwin installed

I have installed Eclipse and CDT (to use C/C++ in eclipse CDT is needed), as well as installing Cygwin so that I can compile my files.
In environment variables I've set Path to include the following: "C:\cygwin\bin;"
g++, make and GDC are all installed via Cygwin. I made sure of this by searching for them in the bin folder - they're all there.
If I enter "make" into the windows command prompt, this appears:
make: *** No targets specified and no makefile found. Stop.
If I enter "g++" or "gdc" into the windows command prompt, this appears (or similar):
'g++' is not recognized as an internal or external command,
operable program or batch file.
So, in other words make is working but the rest isn't..but they're in the same bin folder! Has got me completely confused.
If I attempt to open Cygwin Bash Shell, g++, make and GDC all work there.
However, I need it to work in the command prompt so that Eclipse is able to compile the programs I write in the IDE.
If you know why this is happening, please let me know.
I haven't used cygwin much, but my understanding is you have to use it from the cygwin bash shell.
if you need g++, make, etc, use mingw, with it, g++ works from the normal windows command line.
Here is what happened to me and how I fixed it.
My C:\cygwin\bin\g++.exe is a shortcut pointing to C:\etc\alternatives\g++.exe, which points back to C:\cygwin\bin\g++-3.exe.
Replacing g++ with g++-3 worked for me.
In C:\cygwin\bin see whether g++.exe is there. If not, the installation wasn't done properly and you may need to install again. That's what was my problem and it's resolved :)
Adding the cygwin paths to the Path variable worked for me (windows 8.1 64 bit):
Go to system properties and select advanced system properties
Go to environment variables and select Path in system variables, click edit
Add the Cygwin paths...
for 32 bit:
C:\Cygwin\bin;C:\Cygwin\usr\bin
for 64 bit:
C:\Cygwin64\bin;C:\Cygwin64\usr\bin
In the CMD window, try typing bash to start a bash shell in that window. If that doesn't work, then the cygwin bin directory is not on your path.
If it did work, enter type g++ and type make to see the paths that are being used for these commands. I'm pretty sure your problem is with your PATH variable.
You could install a Windows version of which to get some help in figuring out exactly which executables are being run.
Perhaps it's picking up 'make' from somewhere completely different, and your addition to %PATH% is not working.
Also verify it by typing echo %path% in the same command prompt window as you're seeing the problem in, just to make sure.