Change output directory of an Eclipse CDT project - c++

I cannot find the possibility to change the output of the built files. The only I've found deactivates the whole generated makefile process, which I don't want to.

Right-click on your project and choose Properties.
Go under C/C++ Build, under Settings.
Click on the Build Artifact tab.
Under "Output prefix", enter the directory that you want to contain your built file (including a trailing slash).
It looks like you'll also need to modify your Run / Debug Settings with the updated path.
(However, writing your own makefile really isn't hard, especially if you use Eclipse's generated makefiles as a starting point, and it can give you a lot more flexibility.)

Related

"SDL2.DLL missing from computer" but is in the same folder as program?

SDL Folder in the same directory:
Contents of SDL folder:
I cant get my program to run as it says it cannot find SDL2.DLL? Any ideas?
Programs don't search your entire hard drive for DLLs. That would be expensive, horribly insecure, and bug prone.
Instead they have a search path, which depends on how you load it. Often your PATH environment variable is used, together with various other system places, as well as the directory of the process loading the dynamic library.
This list rarely includes "a subdirectory of the process executable called SDL". You can, however, modify the search path; how exactly depends on how you are trying to load SDL.
The easy solution is to try to copy them all into the same directory.
You can use a Post-Build-Event to copy sdl2.dll into the directory where your .exe is built. Manually copying the file is fine for a quick test, but having Visual Studio do it for you has some advantages. For example, this lets you completely delete your Debug and Release directories and have the correct file copied when you do a clean build.
In your directory listing I see bin32 and bin64 directories inside the SDL directory. I assume that there is a copy of sdl2.dll directly inside each of those directories, but there are no separate debug and release versions, just 32-bit and 64-bit. Let me know if this is incorrect, otherwise, you can do this:
Open the Solution Explorer and select your project (not the solution). Then use Alt+Enter to go directly to the Property Pages. (Or right-click the project and select Properties at the bottom of the menu.)
In the selection panel on the left, go to Configuration Properties/Build Events/Post-Build Event.
At the top of the property sheet, set the Configuration dropdown to All Configurations and the Platform dropdown to All Platforms.
Click the edit box to the right of Command Line and enter this command:
copy "$(SolutionDir)SDL\bin$(PlatformArchitecture)\sdl2.dll" "$(TargetDir)"
Now click OK, and save and build your solution. You should find that it copies the correct version (32-bit or 64-bit) of sdl2.dll into the build directory.
The various $(FooBar) entries in the command line are macros which ar automatically expanded according to things like your project directory location, the type of platform you're building for, etc.
$(SolutionDir) is the directory containing your .sln file, with a trailing backlash.
$(PlatformArchitecture) is 32 or 64 depending on which platform you're building for.
$(TargetDir) is the directory that VS creates your .exe file in.
So this command line is an ordinary copy command as you might use in a command prompt, with the paths filled in to copy either SDL\bin32\sdl2.dll or SDL\bin64\sdl2.dll to your build target directory. We use quotes around the source and destination paths in case you have any spaces in your project path.
In some cases you may need specific commands for a particular configuration (Debug/Release) or platform (Win32/x64). You can do this by selecting that configuration or platform at the top of the property sheet. In our case, we can use one command for both 32-bit and 64-bit builds thanks to the $(PlatformArchitecture) macro.
To learn more about the macros, you can select the Command Line box in the property page and then click the drop-down arrow that appears to the right of the box, and select <Edit...>. This opens a multiline edit window (you can use more than one command in the build event), with an Evaluated value below it that shows the actual command that will be used for your current configuration and platform. You can inspect this command to see if it looks right, and you can also copy the evaluated command and paste it into a command prompt window for a quick test.
To see a full list of available macros and what they expand to for your current configuration/platform, click the Macros>> button below that and look through the list or use the search box at the top.

VS2010: Set what directory an executable looks for files in

I have set my project to output my .exe file into a specific directory, and if I run this compiled executable outside of Visual Studio, it can find and use the files around it using cstdio. However, if I run it within Visual Studio, those files are suddenly gone, as if it's in the wrong directory.
What is causing this, and how might I fix it?
Visual Studio will run the program that's "over there" in its own project folder, so the program's working directory, where it creates and looks for files etc..., will be the project directory and not where the executable is stored.
You can do this yourself. Open a command prompt and type the full path to the executable. It will run, write, and look for files in the prompt's current folder.
Do not use argv[0]. this will give you the command line, and might not include the actual location of the executable.
If you want to keep your files with the executable no matter where the program is run from, GetModuleFileName will tell you where the executable is. You can then strip off the executable to get the path and concatenate that with the names of other files you want to stay in the same folder as, or relative to, the executable. With this approach you can run the program from anywhere, including Visual Studio.
If you want to know where the executable is being run from and make your own way, use _getcwd.
If all you care about is Visual Studio, navigate to Project->Properties->Configuration Properties->Debugging and set the Working Directory to the target directory
I will assume that what you really want to do is to copy the executable elsewhere after the build. That way you don't have to have anything special in the executable that is only for debugging (development). You can have Visual Studio do the copy for you automatically using a Custom Build Step. The build should create the executable in the same project directory that it normally does, then the copy will be done after that (like a mini deployment).
In the project's properties:
Select "Build Events" | "Post-Build Step"
Enter a post build event command to do the copy, for example:
copy "$(TargetDir)$(TargetFileName)" "G:\Temporary"
Where "G:\Temporary" is wherever you want the file to be copied to.
Be mindful of the configuration. You can choose to create the Custom Build Step for both debug and release configurations, but then the executable will be copied to the same place for both configurations. You probably will want a different copy command for each configuration.
While in the property page for entering the Custom Build Step, you can click the down-arrow and select "Edit..." (inside the less than and greater than symbols) to get help creating the command. While doing that, click on "Macros>>" to see the big list of available macros.

Project dependency in Eclipse CDT

I'm using eclipse for the first time. I'm a seasoned VisualStudio user, so I'm trying to find similar functionality in eclipse. I have two projects, A and B. Project A spits out libA.a when it's done compiling. Project B links against libA.a. Here is my problem.
I compile project A then project B, everything is fine.
I make a code change to project A that requires a build of project A.
I try to build project B, but it states that no changes have been detected.
How do I make project B aware of the output of project A?? Currently I'm having to do a clean build of project B for it to re-link against libA.a.
Thanks.
EDIT: In my ProjectB->Path and Symbols->References tab, I have project A checked. This doesn't relink after project A is rebuilt.
Try the below settings:
Go to properties of Main Project → C/C++ General → Paths and Symbols → References
Tick all the dependencies.
You go into Project Properties of Project B, select Project References and make it reference (depend) on Project A.
Edit, appears to be a known bug
One can work around this problem by using the touch command.
In Eclipse, as part of C/C++ Build/Settings is the tab 'Build Steps'. In the pre-build steps command line, enter touch filename.
filename is any file in your application. This could be the file with main(). This could be a special file just for this workaround, touchdummy.c, which can be a tiny file, which compiles quickly.
When the application builds, even if you didn't change any sources, the touch command causes make to rebuild the application. If the library was rebuilt, then the application gets rebuilt with the new library.
One can read about how touch affects the date/time of the file here.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
Edit: The exact command in Eclipse would be touch ${ProjDirPath}/src/main.c
Edit: This command should work, but it appears that if the 'main' project did not change, the pre-build step is not executed. Also the touch command causes eclipse to prompt to reload the file it touched. A large annoyance.
Eclipse projects depend on each other by virtue of the checkbox in the project's properties (dependent projects?) which is how Eclipse decides which to build. You can set this yourself, but it's usually set when you change your Java build path.
By default at least with QNX C++ projects, it WILL NOT check for changes in other projects.
Right click on the project, and expand "check dependencies on/off"->"check user headers only"
It seems to work, roughly...
It appears to do a makedepends on the code, and adds *.d files to the output folder which are simply depends file that list the header files.
Note: these do not appear to get regenerated, and get out of date - I do not know how to regenerate them.
For Project A (library):
Properties>C/C++ Build>Settings>Build Steps>Post-build steps> touch -m ${ProjDirPath}/source/build/build_updated.cpp
You should create folder "source/build" firstly and don't add in that folder any other files.
For Project B:
Properties>Project References> check Project A
Properties>С/С++ General>Paths and Symbols>Source Location>Link Folder>Link to folder in the file system> find and pick "source/build" folder which you created for project A before.

Set Build output directory in Eclipse - c++

I have a program which consists of multiple projects in eclipse (working under ubuntu and the projects being in c++), these projects consist of a main executable file, and other shared objects files and static libs.
I want all these projects when built to output their files to one common binary folder, instead of their respective debug folders. This is to make linking easier with the main executable. If there are better solutions please also feel free to share.
Unfortunately, I have found that the C/C++ Build tab does not allow you to set the build location unless you are creating your own makefile.
You've likely found that the Builder Settings tab under Project Properties>C/C++ Build is all grayed out in a default C/C++ project. This is because CDT nominates the internal builder by default for new projects. To change this, you can go to Project Properties>C/C++ Build>Tool Chain Editor and change the Current Builder to Gnu Make Builder. Next, go to Project Properties>C/C++ Build and change the Builder Type to External Builder. You can now opt to create your own makefile for the project, if you like; though I'd recommend leaving CDT to build the makefile automatically for now.
I have the same project requirements of outputting to a /project_path/bin (though I still maintain separation between Debug and Release builds). To do this, I perform a copy operation on the output as a post-build step.
To do this, go to Project Properties>C/C++ Build>Settings and select the Build Steps tab. In the Post-build steps under Command:, enter:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "/path/to/bin/directory/";
Obviously replacing the "/path/to/bin/directory/" as required.
I personally prefer to maintain my project files in a workspace/build directory; copying binaries to a workspace/bin directory and libraries to a workspace/lib directory. At first I found this copy workaround to be an inconvenience, but have come to appreciate it because it isolates the interstitial build files from the final binary/library.
For binaries, I would use:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "${WorkspaceDirPath}/bin/";
For libraries, I would use:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "${WorkspaceDirPath}/lib/";
I include the variable "${BuildArtifactFilePrefix}" because CDT includes "lib" as a default prefix for static libraries, which I actually prefer.
You just need to ensure that the target directory exists before building; Eclipse/CDT will not create the directory for you.
Also, just remember that these copies will be left behind in the /bin or /lib directory on clean, but overwritten on any subsequent rebuild.
Try Project->Properties
Under C/C++ Build->Settings you have a tab called Build Artifact.
Under there you have Artifact name. This defaults as ${ProjName}.
Modify this to specify a relative directory path to where you actually want the final file to end up. So could be ../../lib/${ProjName}
The intermediate files (.o and .d) will still build to the sub-directory (Debug or Release) but I guess it's better off if they are there anyway and it is only the finally built library for which you want to change the build path.
If you find it inconvenient typing the relative path like this, I use Environment to create environment variables with relative paths taking me back to a "root". One of this I have is ${LIBDIR} and this is a relative path from where the project gets built. It is usually used for linking in other libraries, but can also be used as a target. Then you would set Artifact Name to ${LIBDIR}/${ProjName} which works well if you use different directories for debug and release builds.
Go to
Project Properties -> C/C++ Build -> Settings -> (tab) GCC C++ Linker
The command line pattern is shown on the right side
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} ${OUTPUT} ${INPUTS}
Put in front of ${OUTPUT}
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} ${ProjDirPath}/bin/${OUTPUT} ${INPUTS}
or
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} MyMainProject/path/bin/ ${INPUTS}
From https://www.eclipse.org/forums/index.php?t=msg&th=207500&goto=665566&#msg_665566
In my project the build path defaults to the Build Configuration name, so I could use a ${ConfigName} macro to retrieve the build path in a post-build step:
${workspace_loc:/${ProjName}}/${ConfigName}/${BuildArtifactFileName}
Then you can copy target binaries to your common binary folder, or do something other in the build folder of that particular configuration.
If you open up the project's properties, there is a tab C/C++ Build. This has an option for build location, where you can specify the build directory. It seems you could change this for your multiple projects so that they share the same build directory.
Just happened to be working on something that led me down a similar path - so I'll offer it up as an alternate solution/reminder to myself:
In Eclipse (at least in Luna) the generated makefiles are actually rather decent and convenient. I personally like to create several build configurations (Release and Debug variants with 32 and 64 bit architectures), and supplement them with debug and run (F5 and Execute, respectively) configurations.
To continue: I have been toying with packaging on Debian and found - during the act of said toying - that I needed to create and test an install target. Eclipse neither generates for you, nor provides an interface to - a configuration - for customizing or adding an install target; Other than a place where you can specify that another target exists.
So technically Eclipse does provide an interface; kinda. Hence, I stumbled across the makefile.init, makefile.defs, and makefile.targets files.
Process/Workflow:
Create a file makefile.targets in the root directory of your eclipse project; In said file define an install target manually. This - of course - allows you to specify every little detail as you'd like, but with the added benefit of all of the configuration provided by Eclipse already complete and available to you for use with defining the rules for the specified target.
After defining the new target in the makefile.targets file, right click on your project's name or main cpp file in Eclipse's project explorer, and then select Make Targets->Build..., and finally Add to instantiate a pop-up. Alternatively, you could select 'create' in the last step instead of 'build' and it would provide the same pop-up required for the next part. Add the name of your new target and - leaving everything else at their default values - click ok
If you chose to add the new make target by right-clicking in Project Explorer and selecting Make Target->Build..., after adding the new make target you will be brought back to the first pop-up which resulted from selecting Build.... Otherwise, find your way to the Make Targets->Build.. pop-up now. Select the desired target and then click on Build.
Looking through Eclipse's auto-generated makefiles was an excellent way to learn the makefile syntax and overall structure, and get into some advanced usage of includes and conditionals.
Here are some portions of an example makefile, which - at least I hope - will demonstrate manually setting the output directory of a build:
prefix = /usr/local
bindir = $(prefix)/bin
sharedir = $(prefix)/share
mandir = $(sharedir)/man
man1dir = $(mandir)/man1
...
# Typical all target
all: <binaryname>
#Typical clean target
clean:
rm -f <binaryname> <objectname>.o
# Target invokes all, then installs to specified locations
install: all
install <binaryname> $(DESTDIR)$(bindir)
install -m 0644 <objectname>.1 $(DESTDIR)$(man1dir)

How Do I Use Eclipse to Debug a C++ Program on Linux?

I don't use Eclipse as an IDE, and have no interest in doing so. However, I do like its source-level debugging.
Is there any way I can use it to debug a C++ Linux app without going through the ritual of creating a project? (In effect, can I just use it like a frontend to gdb?)
If not, what are the steps I need to follow to create a project that I can use to just debug an existing C++ program that is built using Makefiles or other tools (SCons, CMake, etc.). I don't want to be able to "develop" in Eclipse; all I need to do is debug.
Take a look at this question. Create a C/C++-project, use your project's source directory as project directory, select to use the external builder, and change "make" to whatever tool you want.
The tricky part is to get the indexer to work correctly and find all your header files.
EDIT: CMake 2.6.x has support for generating CDT project files, which might be a more straightforward solution.
I don't know if this has changed in the 4+ years since the question was posted, but there's a much easier way to do this. I'm on Eclipse Luna (4.4.2).
> eclipse&
then
File > Import > C/C++ > C/C++ Executable > Next > browse to executable > Next > choose a project name > Finish
No other project setup required, no source paths (which should be in the object code). Just like running gdb/insight/etc. Almost makes it worth installing Java.
Configuration for debugging in Eclipse.
In eclipse,
Go to Window->preferences
A popup will appear then select C/C++ , click on drop down arrow ,then select Debug and click on drop down arrow.
Select Source Lookup Path and then click on Add.
After clicking on Add, click on Path Mapping and then click on OK.
Specify the mapping path name and then click on Add .
In compilation path select Cygwin path (need to install) and then click ok .
In debug option,click on source Lookup Path and select Path Mapping:Project source and click on apply and then ok.