I am newly learning c++. I am using the Linux ubuntu operating system and a sciTE text editor.
Do you know how to create a file on scite that I will be able to compile and then create an archive on terminal? also how do I create a 'make file. cpp'. Any help will be greatly appreciated.
Thanks! :)
Do you know how to create a file on scite that I will be able to compile and then create an archive on terminal?
You can use any text editor to write C++ source code. There is nothing very editor-specific about it. The file itself should be assigned a name that your compiler will recognize as designating a C++ source file. Compilers generally look at the filename extension for that purpose, and ".cpp" is a very common pattern recognized as indicating C++ source. It looks like that's what you're intended to use, so when you save the file just be sure to give it a name that ends in .cpp.
In order to successfully compile, you of course need to write valid C++ source code in the file, and in order to compile it to a program (I assume that's what you meant by "archive") it must contain a valid main() function.
how do I create a 'make file. cpp'.
I think you've gotten your instructions confused. Perhaps the instruction is just meant to convey what I already said about giving your source file a name ending in ".cpp". Alternatively, you may have been asked to create a makefile, which is input to the build tool "make", that could be used to build the program instead of running the compiler directly. You may ask specific questions about make here, but we are not in the business of writing full tutorials. The first thing you should do if you need instructions about make would be to consult your course materials and lecture notes, and / or ask your instructor.
On the third hand, make doesn't even need a makefile in some simple cases. It may be that you are instructed to build the program without a makefile. If you have named the source "prog.cpp" and it contains valid C++ source for a complete program, then you should be able to build that program via the command "make prog". In that case, the resulting program will be named "prog".
Related
Ok, n00b question. I have a cpp file. I can build and run it in the terminal. I can build and run it using clang++ in VSCode.
Then I add gtest to it. I can compile in the terminal with g++ -std=c++0x $FILENAME -lgtest -lgtest_main -pthread and then run, and the tests work.
I install the C++ TestMate extension in VSCode. Everything I see on the internet implies it should just work. But my test explorer is empty and I don't see any test indicators in the code window.
I've obviously missed something extremely basic. Please help!
Executables should be placed inside the out or build folder of your workspace. Or one can modify the testMate.cpp.test.executables config.
I'd say, never assume something will "just work".
You'll still have to read the manual and figure out what are the names of config properties. I won't provide exact examples, because even though I've only used this extension for a short time, its name, and therefore full properties path, has already changed, so any example might get obsolete quite fast.
The general idea is: this extension monitors some files/folders, when they change, it assumes those are executables created using either gtest or catch2. The extension tries to run them with standard (for those frameworks) flags to obtain a list of test suites and test cases. If it succeeds, it will parse the output and create a nice list in the side panel. Markers in the code are also dependent on the exactly same parsed output, so if you have one, you have the other as well.
From the above, you need 3 things to make this work:
Provide correct path (or a glob pattern) for finding all test executables (while ignoring all non-test executables) in the extension config. There are different ways to do this, depending on the complexity of your setup, they are all in the documentation though.
Do not modify the output of the test executable. For example, if you happen to print something to stdout/stderr before gtest implementation parses and processes its standard flags, extension will fail to parse the output of ./your_test_binary --gtest-list_tests.
If your test executable needs additional setup to run correctly (env vars, cwd), make sure, that you use the "advanced" configuration for the extension and you configure those properties accordingly.
To troubleshoot #2 and #3 you can turn on debug logging for the extension (again, in the VSCode's config json), this will cause an additional "Output" tab/category to be created, where you can see, which files were considered, which were run, what was the output, and what caused this exact file to be ignored.
This messed with me for a while, I did as Mate059 answered above and it didn't work.
Later on I found out that the reason it didn't work was because I was using a Linux terminal inside windows (enabled from the features section) and I previously had installed the G++ compiler using the linux terminal so the compiler was turning my code into a .out file, for some reason TestMate could not read .out files.
Once I compiled the C++ source file using the powershell terminal it created a .exe file which I then changed the path in the setting.json as Mate059 said and it showed up.
TL;DR
Mate059 gave a great answer, go into settings.json inside your .vscode folder and modify "testMate.cpp.test.executables": "filename.exe".
For me it also worked using the wildcard * instead of filename.exe but I do not suggest to do that as in that might mess up something with the .exe from the main cpp file and what not.
I was wondering how to convert my source code to a working application.
What I mean is after I write my code I want to make app out of it. So I can use it with out a compiler running it.
After you compile, an executable file is created in the location the source code is saved to. For example, if you save your source code to a folder named C++, then in that very folder, an executable file is created which has the same name as the source code. Here's an example, I saved my source code as "aa" into the folder "C++". Now as you can see in the image, there are two "aa"'s, one is the source code, while the other is the executable program ( Well, this is the case in most C++ compilers anyway ).
Well, hope this helps you :)
I'm writing a program using Xcode for school that requires we use the open() system call. I do
int input_file_desc = open(input_path, O_RDONLY);
printf("input file desc %d:\n", input_file_desc);
and it comes up with a -1. The file's path is ~/data_to_read. I set up the command line arguments in xcode. input_path is a const char * that i get from the command line. For some reason it works fine if I change the filename and command line argument to ~/data_to_read.txt. Let me know if more info is needed. thanks.
EDIT: I only tried it with .txt to see if that was the problem, but I still don't know why it needs an extension in the first place. You can have files without an extension right? In which case it should still work, as long as neither the file path nor the argument has an extension, right?
It looks like you are using C, why the C++ tag then? is it allowed? there are easy and good classes in C++ to manipulate files.
However you definitely need to include the extension in the file name (the input_path) , as many files with the same name and different extensions can exist in the same directory, so which one should be opened?
EDIT: it should be known that file extensions are (especially in UNIX-Like OS) only a "helping" thing, they are not really essential. For Example, you could have a file that contains a C++ code but has no .cpp extension, for example its name is foo only with no extensions (or even has a crazy extension like foo.bar). Still you could use the g++ to compile it, because the extension is not really important as long as the content is valid for the application that uses that file.
The way I understand your question, you're asking if a file needs to have an extension to be opened by the C open function, right? The answer to that is no. C does no magic for you, and will attempt to open a file with the exact filename you have specified. If the file has an extension, you must specify it in the api, if not you should not.
Check the error code returned in the errno variable (use strerror or perror to get a human readable error message) to find out what is wrong. That should point you in the right direction.
As other said the extension is simply part of the name.
The problem you are experiencing is probably because your OS is windows and the guys at microsoft had this dead stupid idea of "hiding" the extensions by default so your file seems to be named "test" while indeed it's named "test.txt" because it was created with notepad.
On windows systems is also common to see files named "foo.txt.txt" because this totally dumb idea of hiding/showing/guessing/automatically-adding extensions doesn't work well at all.
You can set the preferences on your computer so that file extension is always shown and this is the best thing to do on a windows system. Even better if you are interested in programming you may consider to switch for programming to an environment like linux that is less hostile to programmers.
A filename doesn't need an extension -- a file can be called "data_to_read". But most filenames on your system do have extensions, it's just that Windows Explorer hides them from you. To open a file from a program, you need to specify this extension.
Right-click on a file in Windows Explorer and select "Properties" to find out whether a filename has an extension. Or look at the files in a Windows Command Prompt console.
I'm just getting my feet wet in C++ using the Stanford CS 106B lectures available online. The assignments have the students use some custom libraries which are available for download online, although the installation instructions are gone.
While I can do the assignments in Xcode using a pre-built blank project which includes the relevant files and source trees set up, I also have TextMate on hand and thought I'd like to try coding with it, since I liked using it a lot for coding LaTeX. So far so good.
The first program I'm trying to run (a very simple ten-line program) contains an # include "genlib.h" in the first line. I have the genlib.h file, but can't seem to get either of the following to work:
Add the path to the relevant file in TextMate: When I try to add the path to the folder on my desktop (/previouspathinthelist:/Users/me/Desktop/C++\ libraries) where the file lives I get an error: /Users/me/Documents/c++ programs/powertab.cpp:9:20: error: genlib.h: No such file or directory even though the file is right there! (Maybe I should note here that the file to be imported and the program file are in two different folders).
Add the file to one of the other paths: I can't move the files using mv in terminal to usr/bin, usr/sbin, etc. because it says I don't have the proper permissions.
Is there something I'm doing wrong in setting my path to my folder in Documents? There aren't any spelling mistakes or anything since the path came straight from get info in the finder. I know this is a programming forum and not a TextMate support forum, but I thought it'd be good to know where people generally put these kinds of files on their systems.
Just put the file in the same directory as your other source files.
#include "filename"
searches the source directory first, whereas
#include <filename>
only searches the include file path.
The reason why /previouspathinthelist:/Users/me/Desktop/C++\ libraries doesn't work probably has to do with the space in the file name. It is quite possible that a backslash is not the right way to quote the space in the tool you're using. Many tools from the C/unix tradition deal rather badly with pathnames that contain space (even though the Unix kernel itself has no such problem); often you'll find that there is no single amount of quoting that will simultaneously satisfy all the tools and subsystems that use some setting. Better to avoid spaces in filenames entirely when you're doing development.
I want write simple C IDE,I wrote some parts of it.
It can check syntax of C,now I need to link my program to gcc,to make executable file.
Now my program can get what you have entered ,then save it in any format(like notepad)
Now I want to say g++ to make executable file,how can I do it?
(For E.g) when user save his/her file then type 'compile it' in my program ,g++ do it and save executable file where program want or shows errors.
How can I do it?
I want to program it for Windows OS.
You don't need to "link" to gcc, instead you will invoke it in a sub-process. This is generally done using fork() and exec(), supplying the full command line to gcc so it knows what to compile, how to compile it and where to store the object or executable file result.
You don't mention your Operating System, but as you mentioned gcc I will assume Linux; have a look at this tutorial, but there many more out there.
GCC can only be invoked from a shell, for compiler functionality inside an IDE look at the libclang project:
http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf