How do you run programs through TextWrangler? - c++

I have just started to get experiment with C++ on my MacBook Pro, and am very new to it all. Through research, a few different people have suggested to use TextWrangler for writing the code. I have put together some of the code that the beginning of a tutorial has given me, but I am now unsure how to run it through TextWrangler. I know that you are supposed to compile it first, but that is mostly where I'm stuck. I have downloaded Xcode so I have gcc, but I don't know where to go from there. And after I have compiled it, is there a simple way to run the file? Like I said, I am very new to all of this and may be looking at it in the wrong way.

TextWrangler is only a text editor, you still need a compiler to compile your code and then you need to run your code.
Xcode (which is an IDE) does all that for you so the confusion is understandable.
for now opening a terminal and doing g++ filename.cpp to compile and ./a.out to run your program will get you going. But you really have to learn to use a IDE or how to write a makefile.

Related

VScode says there are include errors when I try to write c++ programs

I haven't been able to compile c++ programs on vscode (or any IDE for that matter) and I can not for the life of my figure out why. This happened after I factory reset my computer due to other issues. Everything was working fine until then. I re-installed gcc through MinGW and checked that it is installed by typing gcc in command prompt. I can even compile programs through command prompt, which proves that gcc is installed. I would much prefer to use code though, and I was wondering if anyone knew why code is complaining about include paths.
Here's an image of the relevant file with the paths
ANY HELP IS GREATLY APPRECIATED!!!
Install C++ extension, you can find more information here:
https://code.visualstudio.com/docs/languages/cpp

Using g++ and ./a.out

Hello everyone I have finally decided to learn to code, even if it is just a hobby, I once picked it up years ago and I am gong through the tutorials on codeacademy, I have a question for everyone.
Tonight I downloaded Codeblocks with Ming compiler, the programs work fine, the ones I have tried to rewrite from memory from my lessons, but I am missing something that became second nature during my lessongs. After writing the code required for the challenge, I would go into a folder with the extension .cpp and add the command g++ temperature.cpp(for example) -o temperature and then once the a.out file was created I would go into that and do the ./temperature command to execute the program.
I like being able to manually compile and than executre my code, and would like to be able to do this in codeblocks. I know it may sound stupid, and I know Codeblocks does it for me, but is there any way, I can do it for myself? Is there any way I can write my code and have the terminal on the right side of the screen like codeacademy has it?
Or should I just forgot about the g++ and ./ commands? I figure that I can do it throug the command prompt but that would require getting to the folder etc, and have not yet fully familiarized myself with the command prompt.
Thank you in advance for the help.
I first commented, then I decided to make it a full answer, since I kind of answered the heart of the question in the comments by accident anyway.
Yes, you can absolutely do this yourself. Using an IDE (Integrated Development Environment) like Codeblocks will make things easier for you, but there is no harm in learning how to do it under the hood if that interests you--especially if you're doing this for a hobby and not a work situation with time constraints.
To understand what g++ is fully capable of, I recommend consulting the official documentation. That will explain how to properly use it better than me and probably anyone else could.
While you're at it, I would also recommend learning how to use make if your platform supports it. It is a tool that can make compiling easier than working with the compiler directly, but offers more control than your IDE might (though this depends on the IDE).
You mentioned that you used the Ming compiler for Codeblocks, so I'm assuming that you're on Windows. To use the g++ command here, you'll have to
Add it to path, in order to make the g++ commands available for you throughout the system. This video explains adding the ming compiler to the path pretty well.
Call the command (from the command prompt, or some other kind of terminal)
You will have to get just the basic commands down for the command prompt, but it's not that daunting. cd to change directory and dir to list directory, and navigate to your project directory in codeblocks.
Finally, keep in mind that because you're on windows, when you compile with g++ you'll get an .exe file. .out files are produced when you compile on linux. If you want to get a linux shell on windows, I'd recommend looking into either Cygwin or WSL.
IDEs make compiling and executing much more convenient, but it's not a bad idea to learn how it all works through the command line. Best of luck!

Trying to use Eclipse for c++ project on Mac

I am a total noob at programming and IDEs. I am attempting to open a project for some research that I am doing. I have a Macbook Pro using Mac OS High Sierra 10.13.6.
The project that I'm trying to open is a c++ project, but every time I open it, there are error messages everywhere. It appears that the header files aren't even being recognized. A screenshot is linked below. From what I've read so far, the issue seems to be that I don't have a proper debugger for c++. I downloaded XCode after I downloaded Eclipse like some guides have recommended, but it still hasn't fixed the issue.
Other sources I've found on this site use highly technical language (Eclipse GDB MacOSX Mavericks), so I'm struggling to figure out how to fix the problem. If anyone has any ideas, I'd love to hear them.
Here is a better solution for beginners.
Create a file called HelloWorld.cpp
and copy paste the following inside:
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world\n");
return 0;
}
Then open your terminal ( like that ) and write this:
$ g++ -g -o main HelloWorld.cpp
If this goes smooth it means you have gcc installed (good!).
After that try to run this:
$ gdb ./main
If this goes smooth it means you have gdb installed (great!).
Move to create a new Eclipse project with this file only
and update your post if there are any problems.
If this goes smooth gradually migrate your stuff to HelloWorld.cpp.
If gcc or gdb steps above fail, you have to install them first.
There are many online guides on how to do that. Good luck!
I am by no means a Mac expert or do I know much about eclipse CDT or Xcode but I can offer what I know. This is not a complete answer but I just want to share as I also know what it is like to struggle with C code on OSX.
First,
You need to make sure the C/C++ compilers are installed on the Mac properly. They come with XCode and I believe the compiler is Clang. Then you need to make sure the standard libs were installed correctly.
That whole issue is your first problem and should be asked to an XCode programmer and what not.
Second, to use eclipse CDT you need to tell it where and what compiler you are using. As well as the linked and debugger. You can do this in the project properties or settings. The stuff should usually be put in the path variable but once eclipse CDR knows where to find all that and everything is installed correctly it should pick up the header files and then include them in every project!
That’s all I can really provide you and I hope you can find out more.

Getting clang++5.0 to work on Xcode

Clang 5 is finally out there and I have eagerly downloaded it and tried the coroutine example. Everything works nicely from command line and I am very happy about it.
Now, I would like Xcode to use clang5.0 to compile my code, so that I can play with coroutines and other experimental C++20 features with my favorite editor. Is it possible to do such a thing?
I have seen this question and, full of hope, I followed the instructions to replace clang and libclang within the Xcode package.
Xcode starts properly, however when I try to compile an hello world I get a lengthy linker error message that ends with Cannot specify -o when generating multiple output fiels.
Am I doing something wrong? Is there any guides out there to get this to work? Otherwise, I guess I'll just have to change IDE. Sigh.

debugging a c++ program withing R

I'm running a c++ program from R using Rcpp and RcppArmadillo. My OS is windows 8. I use the cxxfunction to compile my code (Later I'll make a package directly instead, but for now I'm clueless about how to make a package). The issue is that R often crash while running my c++ code. There must be some segmentation faults because often it doesn't exactly crash at the same spot even with the same seed. Currently, I'm printing out stuff to the prompt but I feel like this is not the most efficient way to debug my program.
So I read a bit about how to debug c++ code in R and it seems that gdb is a good way to do it. So I downloaded mingwin32 which contains gdb for windows. When I run R using gdb it works fine and I can run all my code fine but when R crash gdb backtrace function returns 'no trace'. I have no idea why. I heard people say to add the option -g -Oo in the compilation, but I'm using cxxfunction to compile my c++ program and I don't know how to compile manually things in R. I also heard some people talk about changing cxxflags but I don't really understand what that is and how to change it.
So my questions are :
Should I use gdb or something else?
How do I get gdb to work with my R program?
Should I install linux on a separate partition to make everything simpler?
Thanks for the help.