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).
Related
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!
As the topic says, I'm interested in using some existing c++ code. It is quite much so I don't want to change everything. That's why I'm asking if I have to change the whole code or if it is possible to include it somehow.
As I noticed Android Studio/Gradle wants a CMake file in order to include those external libraries, but my code only includes ordinary makefiles.
Is there any way to make use of the given structure without many changes?
I can't give too much information about the code because it will be too much but here is something about it:
C++ Code
Using Makefiles
(will add more if needed)
Methods I tried:
Ship code inside assets, copy it in the local directory and use then use "make" to compile it on the smartphone - result: permission denied
Use precompiled Unix executable - I couldn't find a right compiler yet (already tested Android standalone toolchain)
Use Android NDK - I didn't manage to include the existing code because the changes were too deep
In my opinion, the best way would be one of the first two options. But I haven't found a way to handle these problems, which are listed above.
What I would like to know is now:
Which option would be the best? Of course, if they're all possible that will depend on the problem, but some opinions would be nice.
How do I know what compiler I have to use?
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()
The situation is the following: I have the source code of one programm (lets call it programA) (written in C and C++), as well as the CMakeLists.txt and CTestConfig.cmake files. I already installed programA using CMake's graphical user interface and, as it is obvious, it worked. It created the .exe file (I'm working on Windows 7 OS).
The problem is that, right now, I've been asked to edit the program (and so, I must be able to edit the code and degugging it as changes are made). I also need to compile it but not in .exe anymore but in .dll so I can add it to a website we have.
I've read in forums that CMake can compile programA into a .dll if I need to, but as I would need to make some changes I consider that CMake debugging is not as useful and easy as using entirely VS. From the little I know from CMake language, the CMakeLists.txt is mainly used to check the OS of the user as well as adding some libraries in case they are not found.
I have to admit I have no idea in programming CMake directives, as I have been working with ASP.NET, C, C++ and C# mostly. Then, my idea is to try to work only in visual studio 2010 instead of using cmake as well, so once I have the program 'adapted' to VS and can be compiled just using VS, I'm ready to start my job. So the question I have is how can I perform the same task CMake did just using Visual Studio (Is there any way of implementing CMake directives in VS?), can VS compile by receiving as an argument something similar to that CMake.txt file (though it needs to be translated into another language)?
To skip the use of CMake I tried to copy the source code into a new project in VS. However as it does not use the CMake directives when compiling, it gives several errors, most of them related to the fact that some headers.h can't be found (cause they might be in a subfolder). And there are so many subfolders to add the paths to the predefined directories of search that it would take ages.
I'm sorry I can't be more precise in my explanation. I'm good at programming little projects on my own, but it's the first time I have to work on other's programm. Please don't hesitate to ask if anything was not properly understood
I would appreciate a lot any suggestion / advice /guidance you can give.
To make a dll, use add_library command and the SHARED keyword
add_library(mylib SHARED ${files})
this is easy with CMake, don't go back in visual that will be harder at the end
The Good News
Fortunately, cmake can generate VS Projects automaticaly for you (this tutorial s specific for OpenTissue, but Steps 1 to 3 should be the same for you).
The [not so] Bad News
Depending on the complexity of the project, VS Projects automaticaly generated by cmake can get pretty nasty, to the point of illegibility. It will, for example, hard link any library dependencies using the specific paths of your machine, so the project will most certainly not be portable across setups. In any case, that's the intended bahavior, because the primary idea of supporting this generator is simply making it work, thus allowing users to easily compile projects using MSVC, so there's not much you can do here. Nonetheless, it should work in your machine and will certainly be a great starting point for you, just create a project yourself from scratch copying the relevant parts out of the automatic generated version.
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.