#include <iostream
#include <string
#include <fstream>
using namespace std;
int main() {
string file = "mycode.h";
#include file
}
I want to include some code from another file but to user can type location of file than code to be used
This is not possible.
C++ has to be compiled to convert the source code into executable code. #include is a preprocessor command, which is executed at an early compilation stage to combine different source files into a single file that is further processed by the compiler.
The final executable file contains so-called machine-code that is specific for a CPU and an operating system, but in general it does not contain the source code (except maybe for debugging purposes).
Maybe you can solve your problem by an external control program that the user runs and that receives the include path as input, inserts it into the source code, calls the compiler, and executes the compiled executable.
For dynamic include in C++ I think you may need a C++ interpreter. There's one, called Cling:
https://root.cern/cling/
https://github.com/root-project/cling
You are asking for JIT compilation; I think CLing can work for you: https://github.com/root-project/cling
if you check the documents you will see it is doing what you are asking,
ref: https://softwareengineering.stackexchange.com/questions/29344/jit-compiler-for-c-c-and-the-likes
Related
I have a question about how exactly C++ #include statements work. So, let's say you have something like this:
#include <C:\path\to\library\header.h>
An #include with an absolute path.
Now, say you run your program which requires this library. Will it still work on another computer, even if it is not a standard library?
Will the header be included in your .exe file, or does #include search for the header when the program actually runs?
Thank you for your help.
Will “#include”s in C++ still work on other PCs if they have absolute paths?
On the condition that the system where the program is compiled has that file in the specified path, yes they work. However, this is not a reasonable requirement.
Will the header be included in your .exe file, or does #include search for the header when the program actually runs?
Neither. Header files are used in compilation.
Would it be optimizable in a large program if a .cpp file loaded all the needed headers for the application rather than pre-loading it in the main source file?
Like instead of having
Main.cpp
#include <header.h>
#include <header1.h>
#include <header2.h>
#include <header3.h>
//main code
Can I just have a .cpp file that does this and just loads .cpp file in the main.cpp? Like this
Loader.cpp
#include <header.h>
#include <header1.h>
#include <header2.h>
#include <header3.h>
Main.cpp
#include "Loader.cpp"
//main code
Preprocessing simply generates the text that gets compiled. Your suggestion leads to the same body of source code, so it will have no effect on optimization.
Including all the headers, all the time (call it a "super-header") may lead to slow compilation.
However, precompiled headers are a popular solution to allow such super-headers to work quickly. You might check your IDE or compiler's documentation to learn its precompiled header facility.
In any case, typically the super-header is still named with .h; since it implements nothing a .cpp name would not be appropriate.
You can, but you may want to reconsider.
You may have trouble with accidentally trying to compile Loader.cpp by itself since you've named it as if it was a source file. Since you're using it as a header - and it is a concatenation of multiple headers - it would make sense to name it according to the convention and use .h file name extension.
Would it be optimizable
It would have zero effect on the compiled program, so in that sense optimization is irrelevant.
This will bring problems with compilation speed however. And those problems are not "optimizable". Copying every header into the source file - needed or not - slow the time (and the slowdown is bound to the hard drive speed) needed to compile the source file. Much bigger problem is that it prevents incremental building because a change in any header file would force the source file to be recompiled, because its content will have changed.
These problems are compounded over multiple source files assuming you intend to use this "technique" with all source files that you have. Having to recompile the entire project when any header is modified is usually non acceptable for any project that is not trivially small.
It really depends on the compiler.
Visual studio has a thing called stdafx.h.
What's the use for "stdafx.h" in Visual Studio?
I think I can do it in eclipse.
BTW, every time I make a new C++ file with the system default template.
It has #include <stdio.h> instead of #include <iostream>
How to fix it?
Why can't I put many separate source files with MAIN function in one project?
Because your linker can only allow a single implementation of a function per linking object, but you try to give him multiple int main implementations.
Because you did not configure XCode to behave differently, it tries to link all files into one binary object, leading to this conflict.
I think I can do it in eclipse.
Quite surely not, unless you forgot to add the source files to the list of files that end up in your binary, or you forget to define a binary that links everything together.
I need to compile and link some code at runtime. I am using the approach suggested here: https://stackoverflow.com/a/10565120/3038460
Basically I am writing my code into a .cpp file and then compiling with
/usr/bin/g++ -shared mysource.cpp -o libname.so
Everything works fine while I only #include headers from standard libraries. But what if I need to use a custom class within the 'dynamic' code? How can include my own header? The .cpp file is temporary stored in the same location of my binary file and this might be different than the location of my source files. Is there a way to know at runtime where the original source code was located? I doubt.
Moreover I would like my code to work even if the original source code is not available.
To clarify, mysource.cpp might look like this:
#include <stdlib.h>
#include <vector>
#include "myheader.h" <---- how can g++ find this file? At runtime,
when I create mysource.cpp, I have no idea
where myheader.h is located
void f(){
// Code
}
What's the best solution to solve this problem?
Generate the contents of that header file into the generated source file.
Technically, an #include directive is just a directive to include the full contents of a header file in the body of a source file (specifically, at the point where you included it) at compile time. You can thus skip the middleman and generate the contents of that header file into your source file before compiling it instead of an #include
Say I have this very basic c++ code:
myCode.cpp:
#include <library1.h>
#include <library2.h>
int main() {
// use some methods from library2
}
What will happen if I compile this code but my compiler can't find library1.h? Will it throw any specific error? Will it still look for library2.h? Will it skip the rest of the #include statements and go on to compiling main? I'm asking because I'm trying to answer this SO question where adding one .h file is causing the compiler to complain about not finding methods from another .h file . Without the #include first .h line, the code compiles correctly.
When the compiler reaches an include error it reports it and stops the compilation. Of course it may be the case that not all compilers will report a human friendly error, but for sure they all will terminate as this error is fatal as syam points out in his comment.
If a specific #include file can't be found, the compilation fails and stops with an error. Compilation can't proceed any further.
Funnily enough, the behaviour in case of a missing header is not explicitly specified. The only relevant part in the Standard is 16.2/1 [cpp.include]:
A #include directive shall identify a header or source file that can be processed by the implementation.
Note the use of shall which gives no choice to the compiler: it must replace the #include directive by the contents of the file. If the file doesn't exist, the program is ill-formed.
Precisely, if it can't find the file then the compiler won't be able to run the program IF it was needed to run your code correctly. If your code accesses something from that file, the program wont run as needed.