cmake dependency - gcc

I'm trying to create a cmake equivalent to the following make:
demo: main.cpp
gcc -o demo main.cpp
./demo
demo is executed whenever demo is created.
This what I came to, but demo is not executed as I want:
add_executable(demo main.cpp)
add_custom_target(run_demo demo)
This is actually equivalent to:
all: demo
demo: main.cpp
gcc -o demo main.cpp
run_demo:demo
What do I miss?

I'm not entirely sure what you want, as the Makefile snippets you posted do not do what you say they do. But judging by the comment on Kleist's answer, you want the demo to run each time it is compiled anew. You can achieve that as follows:
add_executable(demo main.cpp)
add_custom_command(TARGET demo
POST_BUILD COMMAND ${CMAKE_CURRENT_BINARY_DIR}/demo)

You need to add run_demo to the ALL target:
add_custom_target(run_demo ALL demo)

Related

Corrected usage of gcov on executable and static library

I have a very simple example of using gcov:
library.cpp file compiled and turned into library.a
main.cpp file compiled into executable and uses library.a
All of this done via a simple bash script:
g++ -c library.cpp -Wall -fprofile-arcs -ftest-coverage -o library.o
ar rvs library.a library.o
g++ main.cpp -Wall -fprofile-arcs -ftest-coverage library.a -o main
Now at this point I have the folllowing files (ignoring source and build script):
library.o
library.gcno
library.a
main.cpp
main
main.gcno
So far so good. Now I run my main executable, which creates:
main.gcda
library.gcda
Again all as expected. Now for the question - when I run gcov main.cpp I don't get any gcov files for library.cpp. Is that expected?
Do I really need to call gcov on every source file I may have at any point in time? Is there not a way to call the first source file i.e main.cpp and have it produce coverage stats for each piece of source main goes on to call?
I appear to get gcov files for a lot of core library code that main.cpp uses without need to call them directly with gcov (ostream.gov, locale.gcov...) just not library.gcov
Normally you'd call gcov *.gcno.

How to include *project root* headers compiling with GCC C++ compiler?

How to include headers located in project root compiling with GCC C++ compiler?
I want to tell GCC compiler to search for some header files in project root.
I do NOT want to make changes in code and use relative paths in #include directives - e.g. #include "../../myheader.h"
I compile source code I do not own and I do not want to maintain own version.
I do NOT want to specify absolute include path e.g. g++ -c -IC:\root_project_folder .. for obvious reasons.
I have tried: g++ -c -I .., g++ -c -I/ .. and g++ -c -I"/" .. but it does not work.
Please advise.
root_project_folder
|--myheader.h
|--src_folder
|-prog.cpp
The symbol for the current directory is ..
You're looking for -I.

CMake and Makefile: segmentation fault

I'm calling Makefile from CMakeLists.txt. The CMakeLists.txt compiles a main.cpp file that prints "Hello world", and calls the Makefile. The Makefile compiles a test.cpp file that prints "Test". The code is fine. I checked it when I built the test.cpp just with the Makefile.
The problem is when I'm trying to build CMakeLists.txt. The "main" output works fine but the "test" output throws an exception:
"segmentation fault (core dumped)".
Here is the Makefile code:
all:
g++ -shared -fPIC -o test test.cpp
Here is the CMakeLists.txt code:
cmake_minimum_required(VERSION 3.5)
project(Call_Makefile_From_CMake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(FullPath "/home/orz/ClionProjects/Call_Makefile_From_CMake")
add_custom_command(
OUTPUT ${FullPath}/Test/test
COMMAND make -f test.mk
WORKING_DIRECTORY ${FullPath}/Test
)
add_custom_target(
extern_lib
DEPENDS ${FullPath}/Test/test
)
add_executable(Call_Makefile_From_CMake main.cpp)
target_link_libraries(Call_Makefile_From_CMake ${FullPath}/Test/test)
add_dependencies(Call_Makefile_From_CMake extern_lib)
Thank's for the helpers.
I tried to solve this problem for two days and 5 minutes after i asked for help here i solved it.
I removed the
target_link_libraries(Call_Makefile_From_CMake ${FullPath}/Test/test)
line and it solved the problem. Now it works just fine.

rebar compile fails with bitcask - "errno.h": no such file

I am new to Erlang, so i am going through Joe Armstrong's book "Programming Erlang". In chapter 25 there's an example on how to work with rebar. I followed the instructions and created a Makefile
all:
test -d deps || rebar get-deps
rebar compile -v
#erl -noshell -pa './deps/bitcask/ebin' -pa './ebin' -s myapp start
and rebar.config
{deps, [
{bitcask, ".*", {git, "git://github.com/basho/bitcask.git", "master"}}
]}.
Getting the dependencies works, but compiling fails.
The verbose output tells me that this command fails
cmd: cc -c $CFLAGS -g -Wall -fPIC -I"/usr/lib/erlang/lib/erl_interface-3.7.18/include" -I"/usr/lib/erlang/erts-6.2/include" c_src/bitcask_nifs.c -o c_src/bitcask_nifs.o
with this error
/home/user/folder/deps/bitcask/c_src/bitcask_nifs.c:22:19: fatal error: errno.h: No such file or directory
But
find /usr/include -name errno.h
gives me
/usr/include/x86_64-linux-gnu/asm/errno.h
/usr/include/asm/errno.h
/usr/include/linux/errno.h
/usr/include/asm-generic/errno.h
So I was asking myself..
what am I missing?
how can I tell rebar about the depencies on the C libraries and where to find them?
why isn't this configured correctly in the Makefile of bitcask?
Maybe I was searching for the wrong terms, but I couldn't find any solution in the internets.
Many thanks in advance
There are two thing to consider
rebar options
You can set options for compiling C code with port_env option in rebar.config.
comiling deps
Since bitstack is your dependency, it is not compiled with yours rebar config, but with it's own. So if you would like to change anything, you would have to modify the bitcask file.
Fortunately, if you look into config their writen all C compilation is done with environment variable $ERL_CFLAGS. And again, in rebar source code you can see that this flag is responsible for include paths in your compilation.
So easist way would be extending $ERL_CFLAGS in your Makefile before compilation, with something like this
all: ERL_CFLAGS = "$ERL_CFLAGS -I /usr/include/linux/errno.h"
all:
test -d deps || rebar get-deps
rebar compile -v
#erl -noshell -pa './deps/bitcask/ebin' -pa './ebin' -s myapp start
Just make sure that this include works for you, and that you are not overwriting any flags you are using.

How can I create a makefile that will create two separate executables?

I'm very new to makefiles and using one that was given to us for a project, and I'm expanding it a little bit.
This makefile currently takes one arg, the name of the executable you want to make. In the following code, the command make shapes would create me a an executable called shapes.
GCC_OPTIONS=-Wall -pedantic -I include
GL_OPTIONS=-lGLEW -lGL -lglut
OPTIONS=$(GCC_OPTIONS) $(GL_OPTIONS)
.cpp:
g++ $#.cpp Common/InitShader.o $(OPTIONS) -o $#
Now what I've done to my project is created a copy of it and changed some of the data to see how it will effect the output of the program. The original was called shapes.cpp and the other version is called shapes2.cpp.
What I tried was adding the following line under the current g++ line, but to no avail. I tried to hardcode the '2' part of the file name, presumably incorrectly.
g++ $#2.cpp Common/InitShader.o $(OPTIONS) -o $#2
Could anyone help me come up with a solution? Again, I have 2 separate cpp files I want to compile as two separate executables. I don't have a problem changing the makefile to hardcode the file names, but I'm a bit unsure how to do that.
Edit: Is there a reason this won't work either? It's complaining that it's missing a seperator on line 13, where it compiles the second program.
GCC_OPTIONS=-Wall -pedantic -I include
GL_OPTIONS=-lGLEW -lGL -lglut
OPTIONS=$(GCC_OPTIONS) $(GL_OPTIONS)
.PHONY: all original modified
all: original modified
original:
g++ shapes.cpp Common/InitShader.o $(OPTIONS) -o shapes
modified:
g++ shapes2.cpp Common/InitShader.o $(OPTIONS) -o shapes2
Above the ".cpp:" line, add this:
shapes: shapes2
Now the command make shapes will result in the building of both (assuming the source files are present and correct).

Resources