This has to be some kind of newbie question, but I have not been able to find any explaination.
I am running Ubuntu 18, and need to work with some C/C++ files. I've been using TI's CCS which is eclipse based on Windows for years.
I downloaded the Eclipse installer and ran it setting up for C/C++ developers.
https://www.eclipse.org/downloads/packages/installer
I created a new project. There were several different (unexplained) options such as CDT, MESON, MakeFile, ... I have tried several.
Creating a HelloWorld source file, it compiles and runs fine.
#include <stdio.h>
int main() {
puts("Hello World");
return 0;
}
Okay, so far...
Now I add a new source file. Called "OtherFile.c"
#include <stdio.h>
void OtherFunction() {
puts("Other Hello");
}
And of course, modify the original:
#include <stdio.h>
extern "C" void OtherFunction();
int main() {
puts("Hello World");
OtherFunction();
return 0;
}
When I try to build, it will not compile the new file. And (as expected) it tell me that "OtherFunction" is unresolved.
I have tried multiple project types (CDT, Meson, Makefile) even though there is no explanation of the differences. The newer file will not be compiled.
I tried changing the file extension from c to cpp and back. The newer file will not be compiled.
The TI version of CCS using Eclipse will include a source file when it's in the folder. However, in this environment, I cannot convince Eclipse to compile any other file than the one that was originally created by the new C/C++ project step.
And just as annoying is the fact that I can't right click either file and "Build Selected File". The menu option doesn't even appear.
This did not work for me:
eclipse c/c++ CDT build just one file
Can someone advice how to convince Eclipse to compile additional files?
TIA.
EDIT:
I can't upload here, so I just created something on GitHub.
These are two of the samples where I added a second file, and it ignores it.
https://github.com/scotty2541/EclipseExample
In all the other things I've done in Eclipse, it simply uses a default "recipe" like make does to compile the file.
If there is some way to manually tell Eclipse about it, that isn't explained anywhere I've been able to find. And seems to defeat the purpose of the IDE's behavior.
I was able to get it to behave as expected: By choosing a CDT managed build system, when adding a file to the project, it compiles it using the default recipe
Then, there is a setting which causes it to run the "builder" after a clean.
When I added the file as described originally, I also had to do a "clean" in order for the environment to include the additional file.
Related
Windows 10
Eclipse IDE for C/C++ Developers
Version: Kepler Service Release 2
Build id: 20140224-0627
I'm trying to use imgui. Unfortunately, documentation is pretty shady about how to get it working.
So, in order to get it working, I think I need something called "glad". Whatever it is, the only source of it seems to be some shady-looking one-pager that generates the files for you (found a link to it here).
Anyway, I checked all the boxes I needed (copied from the screenshot from the provided link). I don't understand much about what it is (very roughly), I simply want to get imgui running (ha-ha on me, imgui also needs some other stuff scattered around internet, but it's my next problem, not current).
Anyway, in my new C++ project in eclipse I created a folder "include" and added its path into "C/C++ General -> Paths and Symbols". So when I write "#include "glad/glad.h" in my "test3.cpp" (file with main function, project called test3), I'm ok. But I can't build it because glad.c, which is located in the SAME folder as test3.cpp, has the identical include line, but it gives an error that it can't find it. Sounds like some nonsense to me.
#include "glad/glad.h"
#include <iostream>
using namespace std;
//for those who wanted to copy default hello world program
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
How is this even possible? Looks like a total nonsense to me. They're in the same folder. Test3 sees the file, glad.c doesn't. I tried replacing "<>" in include in glad.c with quotes like in test3.c, same thing. One file works, another doesn't.
What can I do about it?
Eclipse separates the include paths for C and C++ files. If you have a program combining both C and C++ files, the correct include paths need to be set for both languages.
Navigate to the C/C++ General -> Paths and Symbols -> Includes tab. You will see both languages (and probably Assembly) listed under Languages. Pick the correct language or when adding the path, check the Add to all languages box.
I have started a new solution under VS that has both a managed, UWP code project and a native project. The compiler compiles the native code and delivers a .lib file just fine. When compiling the managed code, the compiler compiles the native code again -- as managed code and spits out errors.
I have another solution that use to work and I have tried to replicate the settings. After a day of comparing the property settings, I cannot figure out why it's doing what it's doing.
Ideas for where to look?
_____ New below_____
I have started a new solution an project and replicated everything. Here's what I've learned.
First, the compile order is different -- there was a missing stdafx.h file and the errors went from infinite to just 25. They are now related to one file, MotionBase. The native project compiles just fine, then the managed project compiles and it barfs on MotionBase. This code sample gives errors "'MotionBase': is not a member of 'BallLib'" and"'input': unknown override specifier".
#pragma once
#include "stdafx.h"
#include "FiniteDiffHelpers.h"
#include "MotionBase.h"
#include "MultiVarSolver.h"
namespace BallLib {
class PathFinderHelper : public FiniteDiffHelper
{
public:
PathFinderHelper();
Line locs;
BallLib::MotionBase output;
MotionBase input;
.....
PathFinderHelper compiled fine in the native project. There are no errors in MotionBase. MotionBase is part of BallLib. Intelisense gives no errors in PathFinderHelper.
Ensure the stdafx.h files are properly inserted in the code. The build order is different so you may get trapped.
Include (#include) the native stdafx.h in the managed pch.h file.
Not all the relevant settings are in the project properties dialog. Also check the properties non-modal window, particularly with the project-to-project reference selected inside Solution Explorer.
There you will find a setting named "Use Library Dependency Inputs" which causes the main project to include the individual source files from the library project, instead of the static library. Make sure this is set to False.
I do not regularly write code. There are times I write code daily for 6 months, and then do not code for up to 2 years. This approach has forced me to keep a bunch or reference code that I (and other much, much better programmers) have written. I refer to this "library" when writing code after a long period; I read it, I execute it, and that is a massive help in refreshing myself. This system has served me very well with Eclipse & Java over the past 5 years.
I am now learning C++ and am using Code::Blocks. I would like to somehow stuff a bunch of C++ files that have main methods into a single Code::Blocks project. I am willing to rewrite the code to achieve this task (if it's reasonable...)
I am not the first to look for a meaningful answer for this issue: https://stackoverflow.com/questions/35917504/how-can-i-make-multiple-programs-in-a-single-project-in-codeblocks and https://www.reddit.com/r/learnprogramming/comments/3opp5r/how_to_run_multiple_cpp_files_separately_in_same/
I do not want to change the IDE or compare it to other IDEs. What I am looking for, is the ability to execute one of hundreds of tiny programs that are in a well organized in an expandable file tree in C::B quickly and easily. If I put each C++ file with a main in it's own project I will have so many C::B projects that it will be unreasonable.
I do understand that C::B is not Eclipse and C++ is not Java, and that C::B is intended to have a single c++ file with a main function per project.
Any answers, and even very creative answers would be very appreciated! Scripts, settings, how to rewrite my code, whatever - if you have a suggestion I would love to hear it so I may consider it.
In the interest of full disclosure, currently I am keeping all my tiny programs in directories and use the O/S to drill through the directories and simply double click on the .cpp file which C::B opens. I am willing to dramatically modify my code to be able to achieve the objective.
Thanks for your time.
To compile and run a single main() file in a Code::Blocks project with multiple main() files:
In the "Projects" tab on the left, right-click on a file that you do not want to compile.
In the menu that appears, point to Options, and uncheck both Compile file and Link file.
This has to be done for all the files that you do not want to compile.
Now, when the project is built and run with F9, only the desired file will be compiled; the others will be ignored.
Note: It is not necessary to create a project in Code::Blocks to compile and run single file. To compile and run single files (without creating projects)
Just click on File -> Empty -> New file.
Save the file with .cpp extension anywhere (not in a project).
To compile and run the file, just press F9 or Build -> Build and run
However, such files (without projects) cannot be debugged. The most appropriate thing would be to just have a project with multiple main() files, as explained in the early part of this answer.
I've learned Java before, and am starting to learn C++ now.
When using Code::Blocks as my primary C++ IDE, I met the same problem as you. Although I found nothing useful on the Internet, I managed to figure it out on my own. Here is my solution.
In your Management panel, find your current project and right click, choose Close Project. (We won't use project to manage our code for this purpose.)
Still in the Management panel, find the Files tab, navigate to your working directory, right click on it and choose Make Root.
And we are done!
When you want to add a new code file, right click on your folder and choose New file..., enter your file name with the extension of .cpp. Then you can just use your C::B as before. Without the limit of project, C::B will just compile your current C++ file and run it on its own. Keyboard shortcuts F9 and the Run and Build button still works.
The only disadvantage is that you can see .exe and .o files in your file list, which is a little untidy. I'm still trying to find how to hide them in the list.
Hope this will help you.
How about using the precompiler? You can surround each main with:
#ifdef EXECUTE_EXAMPLE_1
int main() { return 0; } // example of one of the "mains" in one cpp
#endif
#ifdef EXECUTE_EXAMPLE_2
int main() { return 0; } // another "main" in an other cpp
#endif
#ifdef EXECUTE_EXAMPLE_3
int main() { return 0; } // yet another "main" somewhere else
#endif
And creating a header, included by all "mains" where you can define one to run:
#ifndef _EXECUTION_HEADER_H_
#define _EXECUTION_HEADER_H_
// Uncomment one and only one
#define EXECUTE_EXAMPLE_1
//#define EXECUTE_EXAMPLE_2
//#define EXECUTE_EXAMPLE_3
#endif // _EXECUTION_HEADER_H_
This could be a quick and dirty "build system" for your usecase.
I think this is what they were trying to accomplish.
No, you can't have two main files in the same Project, but you can have a lot of smaller project programs saved to a workspace which allows you to run and test them each individually.
https://www.youtube.com/watch?v=cHGIIp3rGO8
I am really new to C++, and I am trying to create a class in a separate file, and I ran into a problem. I basically copied the tutorial http://thenewboston.org/watch.php?cat=16&number=15 from the newboston word for word. However, for the things don't work. I am getting this error when I am trying to run the main file:
C:\Users\Akavall\Desktop\C++ Stuff\New C++ stuff\class_try.o:class_try.cpp|| undefined reference to `Burrito::Burrito()'|
||=== Build finished: 1 errors, 0 warnings ===|
Also, when I am creating the class. The Workspace icon is sitting by itself, while it is supposed to (I believe) include my .cpp and .h folders of the just created class.
My guess is that my paths are not set correctly somewhere, but I have no idea how to fix that. Any suggestions?
Here is the code that I am using:
Main file (class_try.cpp)
#include <iostream>
#include "Burrito.h"
using namespace std;
int main()
{
Burrito bo;
return 0;
}
The class files:
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito
{
public:
Burrito();
};
#endif // BURRITO_H
Burrito.cpp
#include "Burrito.h"
#include <iostream>
using namespace std;
Burrito::Burrito()
{
cout<<"something silly"<<endl;
}
What should I do to fix the this problem?
Thank You in Advance
Edit:
I am using CodeBlocks, and I am on 32bit windows.
If you are using MSVC like that tutorial you linked, you need to first create a project. After creating the project, add all the header (.h) and source (.cpp/.cxx) files that you want to include/compile in it. Now, MSVC makes this easy from this point; you can simply compile (as long as everything is properly added to your project) and it will compile things in order for you.
Extra Information
The information below is not directly pertinent to this issue (or at least doesn't seem that way since you're on Windows), but is intended to help your growth as a developer if you want it.
If, however, you ever decide to use a command line compiler (i.e. MingW) or work in a *nix environment, it is important to note that you need to explicitly compile each object (without linking; in gcc/mingw this flag is -c). Then at the end of compiling several unlinked object, you compile all the objects and the main source file together (with linking this time) and it will create one executable.
I would recommend, however, before trying a command line compiler that you get comfortable/familiar with doing this process through an IDE. Since it is generally an automated process anyway (i.e. most developers end up writing Makefile's for this kind of thing, realistically) you do not really lose too much (immediately, anyway) by not doing this. It is just something to be aware of as you learn.
Using command line compiler you can try this:
g++ class_try.cpp Burrito.cpp -o executable_name
This will link your two .cpp files and create an executable file and you can run that executable file just by typing
./executable_name
I am working on a C++ project using Xcode on MacOS X, and am now starting to port it to Linux using the Code::Blocks IDE.
Many of my source files are in separate directories and I am having issues including them.
Here is an example of this issue:
folder1/foo.h
folder2/dog.h
foo.h includes dog.h with: `#include "dog.h"`
It works fine on Xcode if both files in the same project but if I try it in Code::Blocks it has an error finding it.
I can fix this issue in Code::Blocks by changing the code to use a relative include path such as:
#include "../folder2/dog.h"
Unfortunately doing this stops Xcode from being able to find the file.
How can I fix this issue so I can compile the same code in multiple IDEs? I would like to avoid throwing all the source in the same folder. Should I use a preprocessor statement similar to:
#if XCODE
#include "dog.h"
#else
#include "../folder2/dog.h"
#endif
Rearrange your structure so that one project has only one common include directory:
/project/
/src/*.cpp
/include/*.hpp
/folder1/dog.hpp
/folder2/cat.hpp
Now say #include <config.hpp> and #include <folder1/dog.hpp> etc., and add to your compiler flags:
-I ${PROJECT_DIR}/include
How a given compiler/IDE locates dependencies is, unfortunately, entirely compiler/IDE-specific. There is no way to arrange this in such a way that it will be honoured by all development environments.
I don't know Xcode or Codeblocks, but I'm sure there must be some project configuration that controls where they looks for #include files.