Cmocka tests do not show any output - c

I have downloaded the cmocka example files and followed all the instructions. All test files were succesfully generated and I can run them, but no output appears in the console. I have tried to alter the CMOCKA_MESSAGE_OUTPUT environmental variable, tried to write my own tests and compile them, tried to recompile and reinstall cmocka several times - nothing made the tests output anything. I work on Windows 7 32-bit, so I figured to try also cygwin, but cygwin just throws that it cannot find public libraries, so I abandoned this fork of my research - after all cmocka should also normally work in windows cmd. Does anyone know how to make the tests output anything to the console?
EDIT
I'm adding my make info in case there was some problem with compilation/linking, although I don't see any (it doesn't produce any error and outputs correctly the tests.exe file):
makefile
OBJ_DIR = obj
HDR = $(wildcard *.h)
SRC = $(HDR:.h=.c)
OBJ = $(HDR:%.h=$(OBJ_DIR)\\%.o)
CC = gcc
CFLAGS = -I"C:\Program Files\cmocka\include" -I"C:\Program Files\cmocka\lib" -I"C:\Program Files\cmocka\bin" -llibcmocka -lcmocka
.PHONY: all clean
all: tests.exe
$(OBJ_DIR)\\%.o: %.c %.h
$(CC) $< -c -o $# $(CFLAGS)
$(OBJ_DIR)\tests.o: tests.c
$(CC) $< -c -o $# $(CFLAGS)
tests.exe: $(OBJ) $(OBJ_DIR)\tests.o
$(CC) $^ -o tests.exe $(CFLAGS)
clean:
del $(OBJ) $(OBJ_DIR)\tests.o tests.exe
note1: the numerous paths in cflags are put out of desperation - at first I had been using only the first one.
note2: when I try to run this script in Netbeans or cygwin I change del to rm -f and switch slashes. The output is like described above: the make is done without any errors and outputs the tests.exe, but once executed, it throws error about not being able to find public libraries.

The symbol is not exported, see https://git.cryptomilk.org/projects/cmocka.git/commit/?id=7364469189558a8720b60880940a41e1a0d20452

Sorry for digging out this old thread, but i recently stumbled over exactly the same problem. Compiled everything by myself with meson/ninja and did not get any output neither from the test itself, nor from a simple printf.
I solved the problem by using the precompiled library from here.
Just install/start MSYS2 and use
for 64-bit MINGW:
pacman -S mingw-w64-x86_64-cmocka
for 32-bit MINGW:
pacman -S mingw-w64-i686-cmocka
Then I recompiled my hello world test, and output worked as intended.

I had the same problem, and for me it was that I had not properly passed the state argument to the tests. My tests had this signature:
void test_something() { /* ...snip... */ }
but it should have been
void test_something(void **state) {
(void) state; /* unused */
/* ...snip... */
}
After fixing this, the output properly appeared.

your problem is in the tests.c that has the unit tests, not your setup. Show us your tests.c file where you wrote your unit tests.

I have had the same problem.
Especially I also used gcov to see the coverage and it claims that nothing gets ever executed.
My solution was that I just forgot to add cmocka to my environment-path. After adding "cmocka.dll" to the path everything finally works.

Related

C Makefile with two executables and a common directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I need to make a makefile which compiles two executables, cassini and saturnd
I've been having a ton of problems with this makefile I'm using. Sometimes it compiles, sometimes not.
Makefile
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -Wstrict-aliasing -I include
SRCCASSINI = $(wildcard src/cassini/*.c)
SRCSATURND = $(wildcard src/saturnd/*.c)
SRCCOMMON = $(wildcard src/common/*.c)
OBJCASSINI = $(SRCCASSINI:.c=.o)
OBJSATURND = $(SRCSATURND:.c=.o)
OBJCOMMON = $(SRCCOMMON:.c=.o)
EXEC = cassini saturnd
all: objs $(EXEC)
objs: $(OBJCOMMON)
cassini : $(OBJCASSINI)
$(CC) -o $# $(OBJCASSINI) $(OBJCOMMON)
saturnd : $(OBJSATURND) objs
$(CC) -o $# $(OBJSATURND) $(OBJCOMMON)
%.o : %.c
$(CC) -o $# -c $< $(CFLAGS)
clean :
rm -f src/*/*.o $(EXEC)
distclean :
rm -f src/*/*/.o $(EXEC)
Project structure
Include dir:
include contains the .h files of all c files and more.
SRC dir:
SRC contains 3 directories:
cassini* contains all source files that should be compiled only with the cassini executable
saturnd contains all source files that should be compiled only with the saturnd executable
common contains all source files that should be compiled with both cassini and saturnd
Screenshot : https://prnt.sc/26brtpq
make will fill my screen with verbose output. Sometimes it compiles, sometimes not. For some reason.
Without knowing what kinds of problems, specifically, you see it's hard to say for sure. But, one issue is your cassini target doesn't list any of the common objects as prerequisites. That means when cassini is linked it might be using older, not-recompiled versions of the object files (or they might be missing altogether).
You should change your link targets to this:
cassini : $(OBJCASSINI) $(OBJCOMMON)
$(CC) -o $# $^
saturnd : $(OBJSATURND) $(OBJCOMMON)
$(CC) -o $# $^
and see if that works better. If that doesn't help then your makefile looks correct so it must be something you haven't shown us here, and the only way to help with that is to see exactly how you invoke make and some explicit errors; as mentioned above please don't attach, or link to, screenshots: instead edit your question and cut and paste the text, with formatting.
The trick is not to use a makefile. I've been programming for years and still can't reliably make one of those do what I want.
Instead, you use cmake. It has an easy syntax which you can mostly copy-paste from one project to another and generates makefiles which reliably Do The Right Thing™.
To use cmake, you make a file called CMakeLists.txt in the parent directory of src. Put this in the file:
cmake_minimum_required (VERSION 3.9)
project(cassini_saturn LANGUAGES C)
SET(common_files
src/common/g.c
src/common/h.c
src/common/i.c
)
add_executable(cassini
src/cassini/a.c
src/cassini/b.c
src/cassini/c.c
${common_files}
)
target_compile_features(cassini PRIVATE cxx_std_11)
target_compile_options(cassini PRIVATE -Wall -Wextra -Wpedantic -Wstrict-aliasing)
target_include_directories(cassini PRIVATE include)
add_executable(saturnd
src/saturnd/d.c
src/saturnd/e.c
src/saturnd/f.c
${common_files}
)
target_compile_features(saturnd PRIVATE cxx_std_11)
target_compile_options(saturnd PRIVATE -Wall -Wextra -Wpedantic -Wstrict-aliasing)
target_include_directories(saturnd PRIVATE include)
Edit the obvious places so that the files your project uses are each listed individually. It is a best practice to list all the files, rather than relying on wildcards and globs, though if you want to not follow best practices you can use, eg:
file(GLOB SRC_FILES src/common/*.c)
Now that your CMakeLists.txt file is set up, it's time to use it. To do so, you'll run the following:
mkdir build/
cd build/
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
make
You make a directory called build (it could be named anything) to contain all the byproducts of building and compiling your project. That way, you can just rm -rf build/ to get a clean slate. This is called an out-of-source build and it is a best practice.
The line
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
generates a makefile for your project that, when run, will build your project with optimizations and debugging info. Other options are Release (all optimizations, no debugging info) and Debug (no optimizations, debugging info).
The final line, make, runs the generated makefile and builds the executables inside the build directory.

How to make cc look into /usr/local/include for header files

I encountered this problem while installing some python modules in which had dependencies on their own C libraries. The problem is, cc is not looking into /usr/local/include at all for header files. I made it work for one of those (thinking it was a problem of the modules) by adding /usr/local/include as one of the external include directories.
Then, to test, I wrote a simple hello.c file and added #include "fftw3.h" / #include <fftw3.h> and it failed to compile if I didn't explicitly add -I/usr/local/include.
I added a line in my ~/.bash_profile to export the include the directory path to $PATH; didn't work either.
So, my question is, how do I make cc look for header files in /usr/local/include (or, for that matter, in any custom directory) always without passing -I flag?
FYI: I'm using macbook pro running OSX 10.11
If you are using GCC then you have three environment variables you can use:
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
Take a look here.
EDIT: since you specified you are working with OS X (hence Clang), they should be supported too, take a look ad the end here. It's not uncommon to have Clang mimic GCC specs just to help in compatibility.
I think you should invest some time in understanding build systems. For example gnu make. Here, look at this:
CC = gcc
CFLAGS = -Wall
DEPS = primes.h
OBJ = go.o primes.o
%.o: %.c $(DEPS)
$(CC) $(CFLAGS) -c -o $# $<
go: $(OBJ)
gcc $(CFLAGS) -o $# $^
This gives you:
The freedom to add any compiler you want. In your case that would be cc, in this example it is gcc.
use cflags to control to adjust the compiler - in the example -Wall will turn on the warnings
make your build work reproducible
prepare recipe with complex rules for compilation as your application grow
More information is available here.

Using LLDB for debugging C

I'm writing a small C library for some basic polygon operations and I'm trying to use LLDB from the command line for debugging. I am able to run LLDB with my compiled test runner, but I can only see assembly instructions and not C code as I step through.
I've compiled my library and test runner with the -g flag as shown here in this Makefile:
#Define compiler flags
CFLAGS = -g -Wall -Werror
#Define objects
OBJECTS = MASClip.o MASGraph.o MASClipTest.o
tests : $(OBJECTS)
cc $(CFLAGS) $(OBJECTS) -o tests
MASClip.o : MASClip.h MASClip.c
cc $(CFLAGS) -c MASClip.c
MASGraph.o : MASGraph.h MASGraph.c
cc $(CFLAGS) -c MASGraph.c
MASClipTest.o : MASClipTest.c
cc $(CFLAGS) -c MASClipTest.c
test :
make
make clean
./tests
.PHONY : clean
clean :
rm *.o
I can set breakpoints by function name so I don't understand why the code is not displayed.
I've searched around, but I don't see that I'm doing anything different from what the tutorials and other questions say. I must be missing something obvious.
Also, I realise I could just do this in Xcode, but when I write straight C I like to use VIM and it would be nice to be able to use LLDB from the command line.
How do I get LLDB to display the actual C code when debugging?
On OS X debug info is stored in .o files. The debugger refers back to the .o files using a "debug map" in the executable. Looks like you are deleting the .o files before you try to debug, so there's no debug information for the debugger.
Either leave the .o files in place when you debug, or run the dsymutil tool on the executable to produce a linked debug output file (.dSYM.) If you put the dSYM next to the executable (or anywhere that Spotlight searches) then lldb will find it automatically.
Note that if you just give the compiler a list of .c files, it will make a dSYM for you automatically - since it will delete the .o files when it is done - so that debugging is still possible.

C project with two examples, only one seems to compile, makefile issue

I'm working on an open source C project that has two example files to run the library, one called example.c, and one called test.c.
The Makefile consists of the following:
test: test.c src/term.c
$(CC) $^ -o $#
example: example.c src/term.c
$(CC) -std=c99 $^ -o $#
.PHONY: test example
However, when I run make and then do ./test, test runs, but when I do ./example, it doesn't. Any ideas why?
When you just type make the first target is being executed, in your case its test. So you will get only test executable. But if you type make example then example target is executed and you will get example binary. I think you need fresh up with Makefile rules.You can refer this for basic concepts or this for in depth understanding

What is wrong with this Makefile? (header files not found)

I am modifying an old makefile in order to build a C extension for postgreSQL. The Makefile currently looks like this:
PGLIB = /usr/lib/postgresql/8.4/lib
PQINC = /usr/include/postgresql/8.4/server
CC=gcc
override CFLAGS+= $(CFLAGS_SL) -DPG_AGGREGATE
SHLIB = pg_myextlib
SRC = foo.c \
foobar.c
OBJS = foo.o \
foobar.o
all: $(OBJS)
$(CC) -shared -o $(SHLIB)$(DLSUFFIX) $(OBJS) -I$(PQINC)
cp *.so $(PGLIB)
clean:
rm -f $(SHLIB) $(OBJS)
The error I get when I run make is:
common.h:58:22: error: postgres.h: No such file or directory
Which suggests that the include path is not being added (the file exists in $PQINC).
Its a long time since I wrote the Makefile - and I haven't written many since. As an aside, I am pretty sure that 'shared' is not the gcc flag to build shared libs on Ubuntu (my current dev box) - I think the flag should be 'fPIC' - can someone confirm this?
I am runing gcc v4.4.3 on Ubuntu 10.0.4 and compiling for use with PG 8.4
Try moving the -I$(PQINC) from target all to the end of line that starts with override CFLAGS.
Placing -Isomething on the compiler line which turns object files, like those in $(OBJS), into executable will have no effect whatsoever.
You need to do it when you compile the source files.
Since your makefile doesn't explicitly show the rule for processing source files, it may well be using a default one, which is incredibly unlikely to know about PQINC.
You seem to be using the default rules to build foo.o from foo.c, which doesn't have your -I. Try adding the following rule to your Makefile:
.c.o:
$(CC) $(CFLAGS) -c $< -o $# -I$(PQINC)

Resources