Can we write a program in c++ that compile a c++ source code using a compiler ?
for example we have a program that takes the file name and then compiles it:
Enter your C++ source code file name : cppSource.cpp
your program compiled.
output: cppSource.exe
Or:
Enter your C++ source code file name : cppSource.cpp
Sorry. There is no C++ Compiler in your computer.
I do not mean that we write a Compiler.
I mean write a program that compiles the cpp file using a compiler.
How can we access a compiler. and how to detect that in a computer a compiler is installed or not.
Use system function to call any executable, for example g++ compiler:
#include <stdlib.h>
int retval = system("g++ file.cpp");
Return value of system can be checked, and it will be the return value of the called executable if the shell was able to execute it. In this case, usually it will be 0 if a compiler exists and the code compiled successfully.
Alternatively (and also to prevent called program output from being displayed), for additional details, it would be possible to redirect the program output to a file and then open that file and parse it's content.
int retval = system("g++ file.cpp > output.txt");
Related
When I generate a preprocessor .i output file from a C or C++ source code file using the /P command line option in any version of a Microsoft compiler, the resulting code will sometimes not compile at all even though the original C/C++ source code compiled fine. For example, when I compile the following code directly there are no problems:
#define VALUE -1
int main(void)
{
int x = -VALUE;
return 0;
}
However, the preprocessor output file "equivalent" is the following, which is obviously not equivalent and is not going to compile:
int main(void)
{
int x = --1;
return 0;
}
So my questions are:
Is there some other Microsoft option I should be using to get a .i file that is always a correct representation of the original?
Are there any compilers whose preprocessor output files are always a correct representation of the original?
If I can't use Microsoft for this my second choice would be minGW, and then clang for Windows. I just want something that produces a correct .i file.
Thanks
A number of answers to this exact error have been put upon this website but I am quite the beginner to C++ and Code::Block so i'm afraid I do not understand them.
I have been following a very simple C++ tutorial that started me out with one simple program that I was told to copy and paste into the compiler.
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
return 1;
}
I actually did not write any of this code so my own syntax errors cannot be an issue. Basically that means I'm out of ideas for troubleshooting. Any ideas as to why I can't run this?
Okay so saving the file as a .cpp worked for the building, but when my program actually runs nothing appears in the menu that pops up in which, I assume, the text is supposed to appear. Again, I'm decent at troubleshooting but this code has been confirmed to work by thousands of others and there must be something else wrong.
Save your file in .cpp format instead of .c format which is default for Code::Blocks. Your workspace(that is the file where you saved this code in) will be renamed to xyz.cpp and you can easily check this fact in the tab.Furthermore, change the cout and cin statements to std::cout and std::cin.
Just to make sure we are on the same page.Goto Settings>>>Compiler.Selected compiler should be GNU GCC compiler. Goto Toolchain Executables tab and autodetect the compiler's installation directory (should be something like CodeBlocks\MinGW).
Code::Blocks compiles using some built-in .dlls and i have sometimes found it needed the dll in the folder with the compliled .exe
if not that, try the console application template
i use TDM-GCC it compiles fine.
Working on a C++ based application, it takes user input and generates a C++ function and compile it to create a .so file and links the function to the main application. Currently had to call an external command "g++" to do it. Wonder if it's possible to call some kind of function, say, "compile" which takes as input an code snippet and produces an .so. More precisely, I need a function that has the following syntax:
sizeOfObjBuf = compile(codeBuf, objBuf);
First parameter is a null terminated string containing a code snippet, the second parameter is the output buffer that hold the compiled code and it returns the size of size of compiled code.
The whole idea is to get rid of dependency on an external program (g++) so the application can run on any Linux system (even when it doesn't have g++ installed).
Thanks.
I'm afraid the answer is "no".
You could implement that function by executing G++ (or some other compiler) in a separate process and waiting for it to finish, but that still requires the user to have a compiler installed.
You can't compile C++ code without a C++ compiler.
I am not going to do the research to figure out how it is done, but I believe the LLVM C++ compiler can be used in this way. All of the parts of LLVM are designed to run as a library, in theory.
OK, a tiny bit of research and I found this: http://clang.llvm.org/docs/LibTooling.html
in my test.cu file (cu file item type is CUDA C/C++)
__global__ void foo()
{
}
void CudaMain()
{
foo<<<1,1>>>();
}
and in my test.cpp file
#include "mycuda.cu"
int main()
{
CudaMain();
return 0;
}
and compilator send me error "error c2059 syntax error ' <' " in test.cu file
Inclusion of CUDA source files in a C++ file doesn't work because this simply makes the CUDA source part of the C++ program code and regular C++ compilers do not understand CUDA syntax extensions. If you still want to keep your CUDA code separate from the non-CUDA C++ code, then you might want to look into separate compilation. CUDA source code can be compiled to regular object files, that can then be linked with other object files to produce an executable.
Modify the C++ code to read:
extern void CudaMain(void);
int main()
{
CudaMain();
return 0;
}
Compile the CUDA file with nvcc, the C++ code with your C++ compiler and then link the resulting object files with nvcc (you may also need to specify the standard C++ library in the link command):
$ nvcc -c -o test_cuda.o test.cu
$ g++ -c -o test_cpp.o test.cpp
$ nvcc -o test.exe test_cuda.o test_cpp.o -lstdc++
Edit: your question is about VS2010. May be you have to create custom build steps there.
Based on the thread here: https://forums.developer.nvidia.com/t/cuda-build-error/52615/4
Your test file extension should be .cu as well, but if you're using MSCV rename does not enough you should create a new CUDA C/C++ source module in your VS project.
Also you should put spaces between the <> operators like.
foo< < <1,1> > >();
Because C++ cannot parse the <<<>>>.
I know this is an old question but I was searching around and it jogged my memory for a solution that hasn't been mentioned.
The nvcc help offers:
--x {c|c++|cu} (-x)
Explicitly specify the language for the input files, rather than letting
the compiler choose a default based on the file name suffix.
Allowed values for this option: 'c','c++','cu'.
So although it's a bit of a blunt tool, you can do:
nvcc my_source.cpp -x cu ...
and it'll compile the .cpp as if it was named .cu (ie as CUDA).
I'm a bit of a newbie to C++, but I have some programming experience. I made a basic program, following a guide I found on t'internet. It compiled with g++ easily, but when I ran it it threw these errors:
./FP.cpp: line 1: //: Is a directory
./FP.cpp: line 3: using: command not found
./FP.cpp: line 5: syntax error near unexpected token ('
./FP.cpp: line 5:int main ()'
I'm using Geany on a Raspberry Pi (but using a command line to run the program as the Geany interpreter doesn't work). Here's the program:
// First program in c++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Any ideas?
Cheers!
It looks like you're trying to execute the source file, i.e. run it as a shell script.
To compile your program you would do something like this:
$ g++ -Wall FP.cpp -o FP
This produces an executable named FP. So you should now see both the original program, FP.cpp, and the executable, FP, in your current directory.
To run the executable (compiled program):
$ ./FP
Calling ./FP.cpp tries to execute the source code, which is not what you want. You need to compile it via g++, and then run the executable (usually ./FP)