compile all cpp files in directory with makefile - c++

I am doing a project which is growing pretty fast and manually compiles the project is getting to much work. Therefore i want a makefile to automize this process. I makefile should only compile all the files from the src directory (and all those files in subfolders).
Project directory is as follows:
-src
--files.cpp
--files.h
--|-dirA
---|-aa.cpp
---|-aa.h
---|-ab.cpp
---|-ab.h
--|-dirB
---|-ba.cpp
---|-ba.h
---|-bb.cpp
---|-bb.h
and a main.cpp in the root directory
So far my makefile gets all the cpp files from the src folder:
SRC_DIR ?= ./src
OBJ_DIR := ./obj
SRC_FILES := $(shell find $(SRC_DIRS) -name '*.cpp')
I'm not sure how to continue from this.

Related

Automatic dependency generation and compilation in non-flat directory src

I'd like to adapt the combined automatic dependency approach to a non-flat directory situation where headers are located under ./include/ and implementation files under ./src/ (while Makefile is in the current directory .). I'd like to hold all intermediate and .d (dependency) files inside a separate directory (potentially separate ./build/ and ./.deps/) . To avoid potential clash for dependency files when more than one file with the same name in different subdirectories of ./include/ and ./src/ can exist, I guess building the same structure as in ./src/ in ./.deps could be a solution.
Apart from adding -I directive to the compile phase in the above linked Makefile, what else should I add?

Eclipse CDT generate makefile with makefile.init, makefile.defs, makefile.targets

I am new to CDT and I am trying to generate the makefile automatically. I notice that it include three files that doesn't exist at all, makefile.init, makefile.defs, makefile.targets.
Just wondering, what do they do? and why are they there?
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk
#Other codes
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
#Other codes
-include ../makefile.targets
The three includes are intended for your sake.
If you need to compile something proprietary manually, or copy files or anything you can come up with before the main program is compiled, you create the file makefile.init in the source directory and put your makefile-stuff in here.
The same applies to the other files just at other times in the compile chain.

How to maintain self-made libraries?

I'm using Qt Creator 2.7.0 on ubuntu 13.04.
I've just recently ran into the idea of using libraries, and they're a whole new thing for me.
I've just figured that I need to have the following in my application's .pro file to use a library of my own:
LIBS += -L<lib's dir> -l<lib's name>
INCLUDEPATH += <headers' dir>
// for example:
LIBS += -L$$PWD/../MyLib/release/ -lMyLib
INCLUDEPATH += $$PWD/../MyLib/src/
As you see, I have all my projects in a folder called Programming (the .. in this case)
Each of my project have .pro and .pro.user files in the root, source files in a sub folder /src and the release in an other sub folder /release.
So, this is what my Programming folder looks like:
Programming
MyLib
MyLib.pro
MyLib.pro.user
src
myclass.h
myclass.cpp
release
libMyLib.a
Makefile
myclass.o
MyApp
MyApp.pro
MyApp.pro.user
src
main.cpp
release
main.o
Makefile
MyApp
However, I figured that I could create a folder Programming/libs/, and add libMyLib.a and myclass.h files inside that libs folder.
I would do the same for all of my libraries, and then I could always include them like this:
LIBS += -L$$PWD/../lib/ -lMyLib
INCLUDEPATH += $$PWD/../lib/
The problem is, I'd get include path for every library stored on my computer and the libs folder would become a mess, especially if there are two headers with same name on different libraries.
I'm really new to using libraries, is there a general solution on how they should be located on your computer, and how to include them into your projects?
You could mimic libraries like Boost and have a directory tree like this:
MyLib
build
Makefile, .pro or .sln file here
lib
MyLib
// your .so / .a here
include
MyLib
// your .h here
src
// your .cpp here
CMake or qmake file here
This way you have an out-of-source build tree (in build/) so that your binary files are not mixed up with your source files.
The lib/ and include/ directories are handy because you can then define an install build target. If you then type
make install
it will copy everything to usr/local/lib and user/local/include so that your App can simply do #include <MyLib/some_header.h> and you can link directly against your library binaries (because you copied everything to a location in your system wide path). There is also no danger of name clashes because you wrapped it inside your own MyLib subdirectory.

Reading files from same folder (c++ program)

I have a program that draws the Earth and it uses the following code to read the texture file:
Images::RGBImage surfaceImage;
surfaceImage=Images::readImageFile("",Vrui::openFile("/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"));`
The way I have it set up it only works on my desktop, but I want other people to have access to my program files and pictures, and be able to get the program working on their computer. What should I use instead of using "/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"
If I add a folder to the same place as my .cpp file, should I make changes to my makefile?
Here is my makefile
VRUI_MAKEDIR := /opt/local/Vrui-2.6/share/make
ifdef DEBUG
VRUI_MAKEDIR := $(VRUI_MAKEDIR)/debug
endif
INSTALLDIR := $(shell pwd)
# Set resource directory: I added this images folder to the same place as my
# .cpp file, but it still doesn't work
RESOURCEDIR = images
########################################################################
########################################################################
# Include definitions for the system environment and system-provided
# packages
include $(VRUI_MAKEDIR)/SystemDefinitions
include $(VRUI_MAKEDIR)/Packages.System
include $(VRUI_MAKEDIR)/Configuration.Vrui
include $(VRUI_MAKEDIR)/Packages.Vrui
# Set installation directory structure:
BININSTALLDIR = $(INSTALLDIR)/$(EXEDIR)
RESOURCEINSTALLDIR = $(INSTALLDIR)/$(RESOURCEDIR)
########################################################################
########################################################################
PACKAGES = MYVRUI
########################################################################
########################################################################
ALL = $(EXEDIR)/NewPlanet
.PHONY: all
all: $(ALL)
########################################################################
#'make clean'
########################################################################
.PHONY: extraclean
extraclean:
.PHONY: extrasqueakyclean
extrasqueakyclean:
# Include basic makefile
include $(VRUI_MAKEDIR)/BasicMakefile
########################################################################
########################################################################
$(EXEDIR)/NewPlanet: $(OBJDIR)/NewPlanet.o $(OBJDIR)/drawShape.o
You should use relative path like Beta suggested. Put a "data" folder on the same folder than your executable and use :
Vrui::openFile("./data/land_shallow_topo_2048.png")
File opening should be relative to the program directory, so you could create a sub directory inside your source dir for pictures. Make sure to let the user know where to place these pictures however,
g-dev#g$ mkdir dat
g-dev#g$ mv pic.jpg dat/pic.jpg
then in source:
::readImageFile("", Vrui::openFile("pic.jpg")
adding the directory in CMake:
include_directories ("${PROJECT_SOURCE_DIR/dat}")
adding the directory in VS:
here
(make sure you're using provided MSVS macros for your file path $(ProjectDir) or $(SolutionDir) )

Makefile and updating files

I'm working on a project with a few directories with my makefile one level above all of them. When I update and of the code/header files and re-make, it says 'Nothing to be done for 'all''. Is there a way to force make to check all of the directories and therefore realise that some components have to be remade? Cheers
Jack
You could forcibly enter the sub-directories each time:
SUBDIRS = dir1 dir2
.PHONY: all
all: subdirs final_target
.PHONY: subdirs
subdirs:
for d in $(SUBDIRS); do $(MAKE) -C $$d; done
.PHONY: final_target
final_target:
echo "Do something here..."
This scheme, to enter all sub-directories, are actually very common.