Eclipse CDT: multiple C++ files in single project - c++

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!

Related

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()

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).

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

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.

what is a project in eclipse?

I've just started working with Eclipse for C++ and I just want to clarify something that I haven't been able to figure out by searching so far. Is a project folder in eclipse only intended to have one program in it? i.e., a what is meant by project is basically a C++ program? And so you're not supposed to store many different programs in one project directory? Am I understanding this correctly?
That is correct, though they are trying to change that:
http://www.eclipse.org/eclipse/development/towards-more-flexible-projects.html
Eclipse requires that the contents of each project be stored in a
single directory on disk. Every file and folder in that directory tree
on disk must belong to the project in the workspace
Project is a project. You could divine you aplication to .dll, .exe, or other stuffs. When you are developing something that could go apart in another solution the you can put apart that piece (.dll for example). When you are developing in Visual Studio you can see that projects are in final a single file that has some own functionality, and the whole program is called solution. Didn't fired up Eclipse for a while, but it goes in the same direction. I can remember that projects in Eclipse were very poor, and meaning of the project was considered with actual language that you use (Java, C/C++, PHP, PYTHON, etc.).
Good definition is that project hold some part of application (if not all of it).

Building/Running Lua from Visual Studio

I'm a total noob when it comes to linking and building with Visual Studio. I would like to integrate Lua into my C++ console application.
Can someone give a step by step on how to do this from getting the Lua dependencies from lua.org, to actually running a "Hello World from Lua" in VS, and all the steps in between.
Finding something like this online has been very difficult since most require prerequisite knowledge of building Lua and such.
Thanks :)
Start with the Lua for Windows package. It will get you a self-contained batteries included Lua installation. Lua for Windows is not an official distribution, but it is well respected by the Lua user community. You can use its lua.exe to gain experience with the language in a Windows environment, and its rich collection of tested extension modules is also available for use.
If you add its include and lib folders to your VS project configuration, you should be able to compile and link against Lua in short order.
One possible complication is that the LfW distribution is built against VC8's C runtime library. If that becomes a problem, then you can either compile Lua yourself as part of your Solution, or get a known good DLL that matches your specific version of Visual Studio from the Lua Binaries project.
Do remember that if you are using one of the distributed DLLs, it will have been compiled as C, and not C++. That means that you must wrap any references to the Lua include files in extern "C" {...} or you will have problems with linkage.
It really helps to have some experience with VS project configuration and building. In particular, experience with mixing C and C++ in a VS project is very helpful.
I heartily recommend following the advice already given about learning C and C++ and mixing the two together. Once you have that under your belt, you may want to check out LuaBind or LuaPlus for connecting C++ and Lua. You can do it manually (and you probably should, at first, to understand what's going on under the hood), but it's more efficient and cleaner, code-wise, to use one of those binding libraries. For debugging purposes, Decoda is a good choice; it can attach to processes started in VS which contain Lua code you want to check.