Multiple main() functions within a Netbeans (C++) project - c++

the problem is the following. We have a (rather large) code base in form of a Netbeans C++ project. On compilation/execution, the program generates/executes a binary which then runs the main program. However, now we need to implement several smaller tools (like compilers, converters, etc.) which use many classes from within that code base.
Is it possible to setup the Netbeans IDE to generate multiple executables from within a single project? Things that would work, but are rather inconvenient cover the following:
Compile the whole project as both an executable and a library. Use the library to create compilers, converters, etc (we would have to write a lot of extra code...)
Create different targets, each having an individual macro defined. Use that macro inside the code to exchange the main function (this is just dirty and it doesn't really solve the problem of pushing a single button to compile the main program and all the tools associated with it).
Any suggestions?

I'm not sure whether I understood you project right, but for the task of building several executables I suggest the following: in the build command of your configuration call a script that builds all the executables you need.

Related

Xcode autocompletion does not work for C++ libraries included via CMake

I have an Objective-C based project with some C++ code. I have included library I want to use via CMake. However, Xcode autocompletion is not working properly for library's methods, classes and etc.
Despite that, project compiles and there are no errors during build after inputting some of the library classes or functions in code. Xcode can also correctly specify the error, if I miss something like required parameters for method call (It will show up build error, telling which parameter I forgot to use).
The problem is lack of autocompletion dramatically slows down the development and I need to fix it.
Considering the fact that Xcode is essentially just another UNIX make with GUI on top of it, I would advice just switching to VSCode because the C++ plugin there is designed to work with this kind of stuff.
In your case you could probably use some automatic cmake -> pbx generator if such tools even exist. Or, of course, do this manually, and configure the compilation out of an Xcode project.

Eclipse CDT: multiple C++ files in single project

I'm on Mac OS Mojave(Version 10.14), using Eclipse Photon. I have a single C++ project(which won't show up in the C++ perspective, only the Java perspective), and I'd like multiple packages/folders for various things. For example:
- MyCPPProject
- School
- Lab01.cpp
- Lab02.cpp
- Personal
- File01.cpp
- File02.cpp
All of the C++ files will have main methods(which is why I can't run them if they're in the same project). All of the files will be "simple," meaning that they will not do anything beyond competitive programming(so nothing beyond cin/cout or scanf).
The Internet says I need a new project for every main method. Does anyone have any ideas so that I can keep my programs in 1 project?
Thanks in advance.
The way Java works, you can have a different static void main(String[] args) for every Java class. Neither C nor C++ work like that: you're limited to exactly one "main()" per .exe.
That, in turn, means that you must define a separate project in Eclipse for each separate .exe. That's just the way it is :)
Eclipse, however, allows you to "group" projects into "Working Sets":
What is a working set and how do I use it?
So if you wanted to, you could organize your projects into "School" and "Personal", filtering out one or the other as you wish.
Finally, there are C/C++ "Online Fiddles" that allow you to easily run small, standalone .cpp files and that you might prefer over Eclipse, MSVS or CodeBlocks:
List of Online C++ Compilers
'Hope that helps!

Link c++ object during runtime?

I'm trying to write my first game in c++, and I want it to dynamically load everything from files. This includes the enemies, and I was wondering if there was a way to dynamically include their code at runtime, instead of linking the on compile, so that the levels are easily interchangeable. Lua might be an option but I have no clue where to start, and dll seems to be Windows-only (and I wouldn't know where to start there anyway). Can anyone help with this?
tl;dr I want to link in code to my c++ game at runtime.
For the Lua approach you first need to choose the version first. Right now there is the major version 5.1 and 5.2. My previous work was using 5.1 and for my new project I decided to update to 5.2, however I found that my favorite script wrapping tool (SWIG) does not work with 5.2. Just something to decide at the beginning, because you do not want to get a version working and then have to change it.
Lua comes with makefile build environment. My first experience of trying to build on Windows was a bit of a nightmare, did not appear to just run out-of-the-box, so I opted to create my own Visual Studio project at the time, and just include all the .C files in the project. There are two files which need to selectively included/excluded depending on how you intend to compile: lua.c and luac.c. If you are planning to embed Lua in your app, then exclude both of these files; they both contain a main() function and are designed to build console apps. Include all the rest of the C files in your project.
You should be able to compile easy from this point.
When you include the headers of Lua, keep in mind that the functions are C functions so if you are including them from C++ you need to wrap the file inclusion inside of: extern "C" {} - example: C++ Lua 5.1 Issue
Wrapping your interfaces in another topic and there are lots of resources available. My favorite is SWIG but there are lots of options, including hand coding the conversion of your C/C++ -> LUA -> C/C++ code. Would recommend just focusing on getting the first part working first, get the interpreter embedded so that you can run a "hello, world!" script from Lua inside your app.
So going by your requirement of crossplatform use and dynamic linking, what you're probably looking for is an environment like QT which has QLibrary: https://stackoverflow.com/a/9675063/453673
But https://softwareengineering.stackexchange.com/questions/88685/why-arent-more-desktop-apps-written-with-qt
MingW is the open-source equivalent for Visual C++, so it can help you writing code for Windows (though if I had a choice, I'd directly use Visual C++). The way dll's are loaded in Windows is somewhat similar to the way they're loaded in Linux, so you'll be able to write code with #ifdef's to do conditional compilation. I've written one such program a couple of years back.
To load a shared library(always with .so as suffix) under Linux, you could use dlopen(), dlsym() and dlclose()

Eclipse CDT : How to manage multiple main() functions in a single C++ project?

I am starting the developpement of a project which will be made of multiple modules. I will validate each of those modules with a dedicated testbench, each with their main() function.
I would like to avoid having one Eclipse project for each testbench and its main() function.
I read about Working Sets and the idea of showing only the files concerned for a particular testbench in my project is what I search for.
However, during the build phase, Eclipse continues building all the project files, not the current Working Set files only. I then have a Linker error because of multiple main() functions.
How can I configure Eclipse to Build the files of a Working Set only?
Is there another way to build multiple programs separately in a single project, i.e. an application and its components testbenches?
It is possible to deal with multiple main functions in Eclipse CDT by writting a custom makefile. It is a potential solution for C/C++ projects dealing with a main application and its components testbenches, for instance.
See here how to specify a custom makefile in Eclipse.
Then, define a main rule (see makefile documentation) building your whole application (without the testbenches) and define one additional rule for each of your testbenches (with its component) to be built.
To summarize : define one rule in your makefile for each of your main function, building the main and its dependencies.

int main() first defined here errors

I just started learning c++ today by doing Project Euler problems. My issue is that in Java, I can organize the problems into their own packages, but how do I do that in c++?
The image below shows my Java/c++ setup. The Java part works perfectly for me, however, I can't do the same in c++ (using folders and namespaces) without getting "multiple definition of main" errors.
How can I fit all the individual problems inside the "Project Euler C++" folder? Thanks
Edit: After hours of searching, I found that this post had the best and easiest solution for my problem
In c and c++ you can only have one main() function. I'm guessing the problem here is that Problem1.cpp and Problem2.cpp are in the same project, but each have a main() function. Look up how to use header files, and then include the header files in your main program.
Your problem has little to do with C++, and much to do with Eclipse (n.b. the NetBeans IDE is no better, and I'm sure plenty of others too). You've got a single Eclipse project, but multiple definitions of main(), each of which needs to go in its own executable file.
See here for more details and some suggestions: Project with multiple binaries in Eclipse CDT
You could also write a single main() which dispatches to multiple other "sub-main" functions based on , say, the first command-line argument. Some programs behave this way, including many version control systems (e.g. git add and git commit which both invoke one program but then farm out the work based on the first argument).