How can I compile C++ code using another C++ program? - c++

I want to create a program that modifies another c++ source,compiles it and runs the exe.
I mean with something like gcc may be I can but on a windows os gcc may not be present.
Is it possible?

I think your options are fairly limited for windows:
Check for an install of a compiler (possibly limit this to a short list) and use that compiler
Bring along a compiler in your application's install package

Cowboy answer:
Only if:
the source code doesn't need a lot of files/headers/libraries
they can be easily collected by your application
the application have connection with some server of yours
The application could:
collect the files in a zip
send them over the wire to an compiler service (accesible vía HTTP)
the server compile it with its own installation
and return the binary executable inside the response.
Of course: it depends on so many variables that seems not very feasible. And the zip+http thing could be difficult from a C/C++ app.

Related

can I run binary code for arm-7 on other devices?

I have an image compiled successfully for a mobile device. I now want to run it on another device. Can I potentially use the compiled binary? I do not care about having all the functionality right now. I just want to call one of the functions in the code and get a response.
Is it possible to add the compiled code as a shared library in a JNI project?
#Alex - Thank.
Your library is built for some runtime, e.g. iOS or Linux or Windows. It cannot run on a different type of system, but usually quite easily runs on another device of the same kind.

Programming Arduino DUE without IDE (Linux)

Is there any current, relatively simple way to compile and upload full .c/.cpp files for the Arduino DUE on Linux?
I'm beginning to regularly run into issues using the boilerplate code they provide around the sketches and so far, there is very little in the way of documentation or alternative IDE support for the arduino 1.5 SDK... That and the official 1.0.5 IDE is hopelessly broken for linux right now (serial port issues among other things).
There is a great example here.
He explains what you need and how to use it to be able to upload to the due from the terminal of a linux box.
He has done a great job in helping you set up an environment to compile and upload your c programs onto the SAM3X8E. He even gives you a makefile and sample code. What more could you ask for?
Give it a try, see if it works for you.
Even though you can program in c/c++ for the arduio, the arduino does not "use" c/c++ code alone per se. When you use the IDE for arduino, a few libraries are linked when compiled to give you the "arduino" functions like setup(), and loop() as well as constants such as HIGH and LOW. The arduino language is based off a language called Processing which is written in c.
If you are having troubles with the Arduino IDE, it might help to download an older version. Check out the Previous Releases page on their website.
If that still doesn't work, you could try to build it from the source code. https://code.google.com/p/arduino/wiki/BuildingArduino
I wanted to do the same thing as I really don't like IDE like Eclipse. And I didn't want to rely on Arduino environment. Just something minimalist under ubuntu.
For libraries, I downloaded the ASF (Atmel Software Framework) here http://www.atmel.com/tools/avrsoftwareframework.aspx
For compiling, I installed gcc-arm-embedded from here https://launchpad.net/~terry.guo/+archive/ubuntu/gcc-arm-embedded. This provides gcc-arm-none-eabi.
The last part is 'bossac', the upload tool (just do an apt-get install bossa-cli).
Then, just use Vim or your fav editor, adapt a config.mk (configuration of the ASF's MakeFile) for your own project and once okay, upload the .bin to your board with bossac.
Note that bossac has to be run as root (sudo) if you want it to detect your usb port (/dev/ACM0).
After few days playing with examples provided by the ASF library, you'll be able to use a small subset of headers files (only basic definitions like registers names, bits function names in registers ...) to help you and do all the rest by yourself. I personally even don't use 'drivers' files anymore. I directly access registers with my own methods to get smaller code.

Reg Executing C++ applications on U-Boot (No OS)

We have developed an application which has so many C++ files. On Linux we were able to execute it.
We have an U-Boot for the MPC8548E based custom board.
Now we decided to go without OS. So, I tried two methods to execute the C++ applications on U-Boot.
1.) Compiled the C++ application with the g++ (C++ cross-compiler) and tried to link with the U-Boot, which is compiled using gcc (The C-Compiler). But I am unable to do that:
The error message I am seeing is:
/ToolChain/host/usr/powerpc-buildroot-linux-uclibcspe/bin/ld: failed to merge target specific data of file...
2.) Tried to compile my application along with U-Boot in the same way the standalone examples are done. I created a separate directory in the U-Boot and tried to compile it. C++ applications are not getting built, but I am able to build C-Applications.
My main intention is to execute C++ applications directly on U-Boot.
Please help me how to do that?
Sorry, I believe it would be more work to get C++ on U-Boot than it would be for you to e.g. go with OS.
The short answer, from U-Boot tech lead:
> Does u-boot support C++ example programs and if so, how can I build one?
U-Boot does not support C++.
Some of the technical background for this: U-Boot runs on bare metal. A U-Boot standalone application would link to U-Boot's exported functions which the application needs. For example, your C++ application would use 'new', your C++ library needs to perform calls to malloc, which in this case would mean going to the u-boot exported function interface (refer to doc/README.standalone).
For the general topic of C++ on bare metal, I have not done that, but found Miro Samek tutorial that may shed light. I think it would be difficult. Porting linux starts to look good in comparison.

Requirements for shipping runtime libraries/DLLs

I have read these two SO questions: Which runtime libraries to ship? and License of runtime libraries included in GCC? - both were very helpful but not quite what I was looking for.
I have always just written programs for use on my own machine, which has never caused me any problems, but now I want to start running software on other machines and I'm wary of the runtime requirements.
EDIT: See below example instead, this was misleading.
Specifically, if I write a C++ program on a Windows machine, compiled with gcc through MinGW, and want to run it on another machine:
Do I have to send the libstdc++.dll with my program?
Is this single file (I assume placed in the executable's directory) sufficient to allow the program to run?
Also, an identical example, except this time it is an Objective-C program. Is sending the libobjc.dll file to the other machine sufficient to allow the program to execute properly?
I am used to running programs on machines which have developer tools, etc, installed, but now I'm looking to run them on general purpose machines (friends', colleagues' etc), and I'm not quite sure what to do!
EDIT: In response to edifice's answer, I feel I should clarify what it is I'm looking for. I know how to identify the necessary DLL(s) (/dylibs, etc) that my programs use, (although I am accustomed to doing that work manually; I had not heard of any of the tools). My question was more "What do I do now?"
A more general example is probably needed:
Let's say I have written a program which has object files derived from C++, C and/or Objective-C(2) code. I have used some Windows API code which compiled successfully using MinGW's gcc. I also have a custom DLL I wrote in Visual Studio (C++).
I have identified which DLL's my program will use at runtime (one of which may be GCC's libobjc.dll, I'm not sure if this would/should make a difference on a Windows machine, but I want to make this as general as possible) - The "prerequisite DLLs".
I would like to run it on my colleagues' computers, most of which run Windows 7, but some now run Windows 8. Starting at the very start for the sake of completeness:
Do I need to transfer the prerequisite DLLs to my colleagues' computers?
What directory should I place them in? (exe directory / a system directory?)
Once in place, will the presence of these DLLs allow the program to execute correctly? (Assuming it knows where to find them)
Are there any other files that should be transferred with the DLLs?
Basically I'm trying to determine the entire thought-process for developing and running an application on another machine in terms of system runtime requirements.
When loading DLLs, the first place Windows looks is the directory that the exe is in. So it will probably work just fine to put the DLLs there.
For the Microsoft DLLs though, I think it makes more sense to ask your colleague to install the Visual C++ runtime, which is a redistributable package from Microsoft. Ideally you would make an installer using something like WiX and it would install that prerequisite for you, but it is OK to just tell your colleague to do it.
Be sure to include a license file with your software if you include DLLs from gcc, because the GPL requires it.
libstdc++ isn't necessarily sufficient. You almost certainly need libgcc too, but actual dependencies are liable to vary with your particular application.
The best way to determine what you need to ship with your application is to load your EXE into a program like Dependency Walker.
Just as an example, I've compiled a test C++ program which simply prints a std::string. As you can see, it depends directly on two modules other than those that come with Windows; libgcc_s_dw2-1.dll in addition to libstdc++-6.dll.
You should remember to expand the tree under each DLL to make sure that it itself doesn't have any other dependencies (if A depends on B, B might depend on C even if A doesn't directly depend on C).
If you're worried and want the strongest assurances, you could install Windows into a virtual machine (VirtualBox is free) and test your application inside it. If you use Microsoft APIs, you may wish to check the MSDN documentation to see with what version of Windows they were introduced and ensure that it aligns with your target minimum Windows version.
Update: As xtofl points out this won't cover libraries loaded dynamically using LoadLibrary. If you want to cover this base, use Process Monitor to examine what DLL files are touched when you run the application. (Add an 'Image Path' criterion with the path to your EXE in order not to get flooded.) This has the added advantage that it covers all files, registry entries, etc. that your application depends on, not just DLLs.

xcode's executable product for c++ project

I've completed a simple numbers-version of the game "Towers of Hanoi" using xcode's command line tool in C++. Being accustomed to PC compilers like borland's and visual-c, I've attempted to "share" the game with others via e-mail by simply attaching the executable product built by xcode. However, the recipients can't run the program as it shows up in a different format - usually machine code, it sounds like.
After a bit of extensive searching, I'm realizing the complexity of building projects within xcode's IDE and the variations on the build settings/ targets, etc.
Anyone know how to build a self-contained c++ executable to be universally run? I don't go outside the STL library for this game. I'd greatly appreciate any help.
thanks
OS X is based on Unix, which uses plain binary files (i.e. no filename extension) as executables. If they have a certain "executable permission," they can be double-clicked to be run as executables, or run from the command line. However, this permission can't be sent over email - it's metadata within the file system itself, and this makes sense from a security standpoint (you wouldn't want spammers sending you executable viruses over email right?). So when the recipient receives the binary, they'll need to run the following command line command on it, assuming "hanoi" is the name of the binary file:
chmod +x /path/to/hanoi
If you really want to package it as an instantly double-clickable application, you'll need to give it a native UI and package it as a .app, then put that .app (which is actually a folder with the .app extension) in an archive to distribute. Sorry if that's more work than you were hoping for. Hope this helps!
Sharing applications across dot releases of the same OS can be notoriously difficult on the Mac (at least, as far as personal experience goes).
In order to be able to share your application with the least amount of effort, you will need to figure out:
What project type is this? Are you using any resources like images etc?
What version of the OS your friends are using? If they are not on the Mac, you're out of luck (or you'll have to recompile for their OS-es).
If they run Mac, check out that you have the same OS versions, if you have developed on Leopard and someone's running on SnowLeopard your application might simply fail. (I also ran into issues between Mac OS 10.5.4 and 10.5.3 so keep your fingers crossed.)
Check out what sort of hardware you are running. Are you building for your hardware (say, MacIntel) only or are you creating an Universal Binary?
Make sure that all resources are packaged into your application bundle. Make sure your application uses only relative paths.
Check if you are not writing to special folders (i.e. use only temp and/or word-writable locations, if you need to).
I wish I could give a more detailed/to the point reply but unfortunately you'll have to figure out some of the answers yourself (without any other specific information about the error you are getting).
If you're satisfied with a command line tool rather than a double-clickable app, it should suffice to zip it and attach that to the e-mail. Be sure to build universal if anyone you're sending to might be using a PowerPC-based Mac. Oh, and set the deployment target to the minimum OS that any recipient might be using.