C++-based make system - c++

Are there any build systems that don't use a DSL but actually use C++ as the build language?

Yo Dawg, I heard you like C++, so I added C++ to your build system, so you have to compile before you compile.

I've written a build system that I use in my projects in Python called pybake. It's designed to be a bit smarter than make, with less magic. The build is also defined in Python, thereby reusing an existing language, rather than generating a new DSL for that purpose. Here's an example of it in use.

None that are popular, if anyone was crazy enough to even write one. C++ would be an incredibly clumsy language for that.
If you're looking to create one, instead pick a language such as Python or Lua in order to use something popular and not invent a new DSL.

Are you asking if there are any build systems, like Make or Ant that use C++ code as the directives rather than specialized commands? While many higher level languages have such a system, there aren't any in C++ that I am aware of. Certainly not the popular ones. This is probably because C++ is a compiled language and not one that is trivial to parse. This makes it less suitable for what is essentially a lightweight scripting task.

There is probably C++ code in there somewhere but if you mean you would have to write a C++ program and compile it then run it to build a different source tree, I don't think that would really work in the scheme of things. What would you build your build script with? It goes on and on.
Compilers and the commands behind the scripting languages are often written in C or C++.

Since C++ is compiled, this would give you the problem of needing a build system for your C++-based build system. I am not aware of any C++-based build system and would be surprised to find one. For interpreted languages like Python, bootstrapping isn't an issue (and so you will find scons, for example).
If you are looking for something better than Make, though, check out CMake. Though somewhat out-of-date, you may find the C++ Project Template helpful as an example for creating CMake-based projects.

Related

build and run c++ within c++ -- c++ scripts (instead of lua)

Currently, I embed lua in my c++ code.
I wonder if the same is possible with c++. That way I can pass objects and use libraries within the script. to do this, I think I would need the following from the master c++ code:
execute the running of the script
compile the script
run the script
the script passes pointer to it's data objects
the script is done and the program uses the data objects.
is this possible? small example?
For me it is useful as I ran science calculation that I don't want to stop but i want to add functionality on the fly. I also see this being useful for servers in c++ that don't want to shut down but want to make a change.
You may choose to use some kind of C++ interpreter but they can't do every thing. C++ is one of the hardest to implement(if not that hardest) languages. Just think about macros, templates and every other thing that make C++ as powerful as we know they are really hard to parse and understand and beside all of that we have optimization that create such fast and thin codes for us, so how an interpreter can be such powerful and understand all of this? Even best known compilers may fail to understand all of the standard or at least have errors for understanding complicated C++ codes. In one of my projects that heavily use templates using MSVC 2010, it take about 10 minutes to compile the code! So interpreters usually simplify their task and only support a subset of full C++, so if you are cool with this use some interpreter. I never used one of them but I heard cint is good.
The other option is you have a free compiler like g++ and actually compile your code into some kind of shared library and then load it dynamically and call some functions from it based on some kind of configuration.

Using scripting language in C++

My question is a little bit stupid but I decided to ask advanced programmers like some of you. So I want to make a "dynamic" C++ program. My idea is to compile it and after compilation (maybe with scripting language like python) to change some how the code of the program. I know you will tell me that after the compilation I can not change the code but is there a way of doing that. Thank you!
You could design a modular architecture using plugins in external libraries. For example you could use the Command Pattern. That way you can dynamically load code that was generated after you main program. You would have to fix an interface though. Functions like GetProcAddress in the Windows api might be a good point to start.
For dynamic coding and rapid prototyping I recommend to have a look at Lua. The engine is very small and easy to integrate in your c++ program.
The only way to do that in C++ is to unload the DLL with the code to be
modified, modify the sources, invoke the compiler to regenerate the DLL,
and reload the DLL. It's very, very heavy weight, and it only works if
the compiler is present on the machines where the code is to be run.
(Usually the case under Unix, rarely the case with Windows.)
Interpreted languages like Python are considerably more dynamic; Python
has a built-in function to execute a string as Python code, for example.
If you need dynamically modifiable code, I'd suggest embedding Python in
your application, and using it for the dynamic parts.
Personally I have never played with re-compiling C++ during runtime, and I do not intend too. However I have been doing a lot of embedding of scripting languages lately.
Someone else mentioned the obvious first choice for embedding: Lua. Lua is a pretty popular language to embed and you will find a bunch of documentation about how to do it. Integrating it into the C++ will allow you to define behavior at runtime like you want.
What I am using is a wonderful langauge called Squirrel. It is a lot like Lua but with native object(class) support and C++-like syntax. I have managed to embed it into a C++ application, and through using the sqrat binding library both languages can share information easily.
I have squirrel building and initializing my UI. As a result, 0 compiling is required in order to craft and structure my UI for my game. I intend to take this a step further and use this gameplay-side in order to create similar behavior you are looking for(changing behavior at runtime through dynamic code usage)
I recommend Checking out squirrel here: http://www.squirrel-lang.org/
I plan on writing tutorials on how to embed squirrel and install the binding library, but I have not started on them yet. If I can remember, I will edit this post in the future (could be a few months) once I have completed them. In the meantime give it a shot yourself. You may find it to your liking.

Is there a library that can compile C++ or C

I came here to ask this question because this site has been very useful to me in the past, seems to have very knowledgeable users who are willing to discuss a question even if it is metaphysical at times. And also because googling it did not work.
Java has a compiler and then it has a JDT library that can compile java on the fly (for example used in JasperReports to turn a report template into Java code).
My question: Does anyone know of a library/project that would offer compiling as a set of library classes in c/c++. For example: a suite of classes to perform Preprocessing, Parsing, CodeOptimization and of course Binary rendering to executable images such as ELF or Win format. Basically something that would allow one to compile c or c++ scriptlets as part of an application.
Yes: llvm. In particular, clang. At least, that's how they advertise the projects. Also, check this question. It might be relevant if you decide to use llvm.
You might be able to adapt something from the LLVM project to your needs.
You can just require that a compiler be installed, then call it. This is a fairly hefty requirement, but about the only way to truly "embed" C or C++. There are interpreters that you may be able to embed, but that currently seems a poor choice, not the least because any libraries used in the script must have development versions (i.e. headers and source/compiled-libraries) installed, and those libraries could be restricted to the feature set supported by the interpreter (e.g. quality of template implementation).
You're better off using a language like Python or Lua to embed.
There is the ch interpreter but I have not used it. Generally for scripting type applications a more natural scripted language is used.
Great. It looks like LLVM is what I was after.
Thanks a lot for your feedback.
I am not primarily after C++ as a scripting language. I have noticed that Python is used as an embedded script engine.
My primary reason is two fold:
Get rid off Make,CMake and the hell that is Autoconf and replace it with something like Scons that binds into and interacts with all phases of compiling
Hook into the compiling process after parsing and auto generate code. Specificaly meta related code. In my case I have been able to implement almost every feature of Java in C++ except one: Reflection.
Why impose on your code uneeded bload like RTTI that is often inadequate. Instead one could selectively generate added features. But developer would have to choice when and how to use this extra code.

Generating C++ code at runtime, compiling and running it

is there a multiplatform c++ compiler that could be linked into any software ?
Lets say I want to generate c++ code at runtime, compile it and run it.
I'm looking for a compact solution (bunch of classes), preferably LGPL/BSD licence :)
As far as I know it can be done in Java and c#. What about c++ ?
Well maybe one of the modules of CLang will be of help? It's not dry yet on the C++ side but certainly will be soon.
I don't know of any open source ones for C++, but if you want small and compact scripting and are not hung up on C++ LUA might be an option for you
I'd drop C++ altogether and use Google V8. If you wanted to use C++ because the people using your app only know this, they should have no difficulties going to javascript.
And it's damn fast. And Javascript is a cool language too.
I've done this years ago in Linux by generating C++-code into a file, compile it by shell execute (with gcc) and then linking in the generated library dynamically. The dynamic linking differs of course between platforms.
This kind of thing is much much harder in C++, because the language doesn't use a virtual machine (or "runtime") that abstracts machine specifics away.
You could look into gcc, it's under the GPL IIRC, and ports exist for all major platforms.
When we looked into scripting we chose AngelScript because of the similarity with C++.
V8 is great but it's certainly limited to some platforms, AngelScript is a lot easier to compile with and probably to learn (if you came from C++) and it has a zlib license.
http://www.angelcode.com/angelscript/

Python non-trivial C++ Extension

I have fairly large C++ library with several sub-libraries that support it, and I need to turn the whole thing into a python extension. I'm using distutils because it needs to be cross-platform, but if there's a better tool I'm open to suggestions.
Is there a way to make distutils first compile the sub-libraries, and link them in when it creates an extension from the main library?
I do just this with a massive C++ library in our product. There are several tools out there that can help you automate the task of writing bindings: the most popular is SWIG, which has been around a while, is used in lots of projects, and generally works very well.
The biggest thing against SWIG (in my opinion) is that the C++ codebase of SWIG itself is really rather crufty to put it mildly. It was written before the STL and has it's own semi-dynamic type system which is just old and creaky now. This won't matter much unless you ever have to get stuck in and make some modifications to the core (I once tried to add doxygen -> docstring conversion) but if you ever do, good luck to you! People also say that SWIG generated code is not that efficient, which may be true but for me I've never found the SWIG calls themselves to be enough of a bottleneck to worry about it.
There are other tools you can use if SWIG doesn't float your boat: boost.python is also popular and could be a good option if you already use boost libraries in your C++ code. The downside is that it is heavy on compile times since it is pretty much all c++ template based.
Both these tools require you to do some work up-front in order to define what will be exposed and quite how it will be done. For SWIG you provide interface files which are like C++ headers but stripped down, and with some extra directives to tell SWIG how to translate complex types etc. Writing these interfaces can be tedious, so you may want to look at something like pygccxml to help you auto-generate them for you.
The author of that package actually wrote another extension which you might like: py++. This package does two things: it can autogenerate binding definitions that can then be fed to boost.python to generate python bindings: basically it is the full solution for most people. You might want to start there if you no particulrly special or difficult requirements to meet.
Some other questions that might prove useful as a reference:
Extending python - to swig or not to swig
SWIG vs CTypes
Extending Python with C/C++
You may also find this comparison of binding generation tools for Python handy. As Alex points out in the comments though, its rather old now but at least gives you some idea of the landscape...
In terms of how to drive the build, you may want to look at a more advanced built tool that distutils: if you want to stick with Python I would highly recommend Waf as a framework (others will tell you SCons is the way to go, but believe me it's slow as hell: I've been there and back already!)...it takes a little learning, but when you get your head around it is extremely powerful. And since it's pure Python it will integrate perfectly with any other Python code you have as part of your build process (say for example you use Py++ in the end)...