How to setup a cmocka example with arm-none-eabi-gcc + cmake? - c

I am developing firmware for stm32f4xx based systems.
For that I setup a toolchain based on the arm-none-eabi-gcc toolchain form ARM and cmake.
This toolchain works on Ubuntu. I can x-compile and debug(via openocd + eclipse IDE).
Now I like to add do some functional testing for my code. I was checking and it seems that cmocka is a good tool to use for embedded software testing.
I am now looking for an example/template that integrates a test into the cmake build.
let's assume a simple function at myfunc.c
#include "myFunc.h"
int buffer[10];
void myFunc(int i, int val) {
buffer[i] = val;
}
if I got it right I can do a test in a separate c file like "test.c"
#include "myFunc.h"
#include <cmocka.h>
// function success
static void test_myFunc_positive() {
for(int i = 0; i < 10; i++) {
myFunc(i,i);
}
}
static void test_myFunc_outofbounds() {
myFunc(100,44);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_myFunc_positive),
cmocka_unit_test(test_myFunc_outofbounds),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Usually I run
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="cmake/STM32Toolchain.cmake"
My question contains some sub questions:
1.) I installed libcmocka-dev. This is for my host system. Do I need to install cmocka for my arm-none-eabi-gcc compiler?
2.) How to setup cmake to pick the cmocka lib, build the test and run it on the host system? Think my toolchain file needs to be ignored.

Your source code looks pretty fine. Here is a recipe of how you could use cmocka.
I recommend to crosscompile cmocka's source code, too. In fact I doing it in this way:
Add cmocka.c to your sources
Add 'cmocka.h and cmocka_pbc.h and cmocka_private.h to your include directories.
Compile and run your software
PS: I don't know libcmocka-dev. I think this is a precompiled version of cmocka?
PPS: I had some trouble on getting cmocka's output redirected to my serial UART. If you're having the same problems, feel free to ask.

Related

Can you have multiple DLLs with common exports/ordinals and exchange at run-time?

I am trying to create a couple of Win32 64-bit DLLs (Windows 10) which have different implementations but consistent symbol exports. The aim for this is that one would link with whichever one at build time but have the option at deployment to install either DLL and correctly run with that. I have achieved this straightforwardly on Linux where I am much more comfortable and familiar with run-time linking. But on Windows, I have not yet managed this and I am wondering if this is possible at all. I am trying this using both VS2010 and VS2019.
Suppose I have two libraries blah_legacy.dll and blah_modern.dll. They both export 6 symbols which are the interface to using the library, e.g. blah_open, blah_read, blah_write, blah_close, blah_control, blah_status.
I can link with the import library for either blah implementation and a test program calling each symbol loads and executes correctly with the corresponding blah DLL.
However, I cannot yet switch the DLLs at run time. For example, should I actually be able to link with blah-legacy.lib and then run with blah-modern.dll if I rename it to blah-legacy.dll? (Or vice-versa.)
I already got around basic file-naming issues and ensured the DLL needed can actually be found. I still got the application failed to start (0x22).
I used "objdump -xs" on the DLLs and noticed the order of symbols and their ordinals are different. So I created a .def file and ensured that the exported symbols match in number, names and in ordinals. Still nothing - the same error occurs.
There's still something to this I clearly have not figured out and would appreciate some guidance. Is this actually possible? Where do I start to look (which tools) to figure out what step to take next.
Yes.
I don't use Visual Studio much, but this is the kind of thing that happens all the time if you use MSYS2, and install some MinGW packages, and update them.
Here's what I mean by that: MSYS2 is an open source software distribution for Windows that, among other things, provides a bunch of native Windows software packages. The package manager (pacman) let's you choose which packages to have in your system, and it downloads DLLs and EXEs that were created by the MSYS2 developers. When an MSYS2 developer updates a library, you can download the updated library package, and all the other packages using that library will automatically start using the new DLL. Usually there is no issue with that because the new library version will be ABI-compatible with the old library version.
You do not need to use LoadLibrary or otherwise mess up your source code; the linker and the operating system should be able to take care of this for you.
Example
Here is a minimal example I threw together with MSYS2 showing how this can work.
The file foo_legacy.c represents your legacy DLL. I added some extra symbols so it wouldn't be too similar to the modern DLL.
__declspec(dllexport) int eoo() {
return 0;
}
__declspec(dllexport) const char * foo_name() {
return "legacy";
}
__declspec(dllexport) int foo_version() {
return 1;
}
__declspec(dllexport) int goo() {
return 0;
}
The file foo_modern.c represents the modern implementation:
__declspec(dllexport) const char * foo_name(void);
__declspec(dllexport) int foo_version(void);
int foo_version() {
return 2;
}
const char * foo_name() {
return "modern";
}
The file main.c represents an application using the foo API:
#include <stdio.h>
__declspec(dllimport) const char * foo_name(void);
__declspec(dllimport) int foo_version(void);
int main()
{
printf("%s %d\n", foo_name(), foo_version());
}
My build.sh file is a Bash script that builds and tests everything:
#!/usr/bin/bash
set -uex
gcc -Wall foo_legacy.c -shared -o foo_legacy.dll
gcc -Wall foo_modern.c -shared -o foo_modern.dll
gcc -Wall -c main.c -I. -o main.o
gcc main.o foo_legacy.dll -o main.exe
./main.exe # output: "legacy 1"
mv foo_modern.dll foo_legacy.dll
./main.exe # output: "modern 2"
rm foo_legacy.dll
./main.exe # fails because foo_legacy.dll is not found
The build script runs main.exe three different times, showing that it can either use the legacy DLL, or use the modern DLL, or fail, depending on what was installed in foo_legacy.dll.

OpenAL Library Linking With cMake

I'm trying to get a grasp on audio programming in C with OpenAL. I prefer CLion as an IDE to Visual Studio, but that generally means having to deal with cmake stuff and I ran into an issue regarding this. Right now I'm just trying to link the library (as in getting the definitions of the OpenAL functions) but it seems I've got something out of place. Here's the CMakeLists.txt file,
cmake_minimum_required(VERSION 3.19)
project(AudioTest C)
set(CMAKE_C_STANDARD 99)
set(OPENAL_LIBRARY "C:/Program Files (x86)/OpenAL 1.1 SDK/libs/Win64/") //OpenAL Installed here
find_package(OpenAL REQUIRED)
add_executable(AudioTest main.c)
target_link_libraries(AudioTest "${OPENAL_LIBRARY}")
The Cmake file compiles (or reloads) fine on it's own. But when I try to run this simple program,
#include <stdio.h>
#include <al.h>
int main() {
printf("Hello, World!\n");
alGetError();
return 0;
}
I end up with this,
undefined reference to '_imp__alGetError'
Could I get some pointers to what I might be missing?

How to run c unit testcases using CMOCKA framework?

I have recently started working on a project written in C language. To add unit test cases, I searched for C unit test frameworks and I came across this answer C-unittest-frameworks. So I chose mocka and installed it from installation steps. But I am aware of how to run the written test cases. Can somebody help me to run from Command line ( GCC command) the simple unit case below:
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h> /* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
(void) state; /* unused */
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
And FYI I am referring cmocka. And please let me what are the dependencies required other than GCC and cmocka. Or any Environment variable needs to set for LD paths etc.
Update on this question, i followed following steps:
1. git clone https://gitlab.com/cmocka/cmocka.git
2. cd cmocka && mkdir -p build && cd build/
3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Debug -DUNIT_TESTING=ON ..
4. make && make install
After all, steps, if in build dir I execute ctest tests/, it is running all the test cases. Now I am stuck on how to follow the same steps for my project and run a dummy unit test case.
Finally, after more research on the internet, I came across this very well written article on how to run unit test cases using cmocka. Here is the link run c unit test cases using cmocka.
I also forked his git repo & modified it accordingly, have a look at git repo also git code.
Also, I tried another framework gtest, have a look gtest example code

Link against a Windows .dll+.lib file combination with GCC under Cygwin?

I know how to link against libraries in Unix-ish contexts: If I'm working with .a or .so files, I specify the root search directory with -L/my/path/to/lib/ and for libMylib I add -lMyLib.
But what if I have
a .dll (e.g. in the Windows\System32 directory)?
a .dll (in Windows\System32) and a .lib (someplace else)?
These DLLs are by some other party; I don't have access to their sources - but do have access to the corresponding include files, against which I manage to compile.
If you can link against a .lib in Cygwin or MinGW, then you can (indirectly) link against a DLL.
In the MSVC world, it is not unusual to create an import library along with a DLL. It is a static library (.lib) that loads the DLL and wraps the interface of the DLL. You just call the wrapper functions in the (static) import library and let the import library do all the DLL-related things.
For the Windows API, there are import libraries in the WindowsSDK.
For your own MSVC DLLs, MSVC can automatically generate the import libraries when you build the DLL.
For a third party DLL, you can build a static wrapper library based on the corresponding header files.
Linking against the .lib file in Cygwin or MinGW is possible. Example:
g++ -o myprg myprg.o -lShlwapi
This links against Shlwapi.lib. (The library must be in the local directory or in the library path of the linker.)
Linking against import libraries of DLLs works the same way.
Note 1: Keep in mind the different ABIs and name mangeling. However, calling plain C functions in DLL or LIB files will work in most cases.
Note 2: Keep in mind that g++ requires the libraries to be specified in the correct order.
#einpoklum Converting my comment to an answer: #n.18e9 is correct in that you must use the full path name for the lib file without any -L or -l options.
g++ -o foo.exe foo.o c:\something\somethingelse\some.lib. You can also link directly to the Windows DLL file g++ -o foo.exe foo.o c:\something\somethingelse\some.dll.
Important - make sure you are linking to a lib file (and associated dll) generated for a 64-bit platform (on MSVC target X64, not Win32).
OK you wanted an example, well let's go.
Here are two examples using gcc/g++ to link to a Windows native DLL which exports plain C functions (using here x86_64-w64-mingw32/8.3.0 on Windows 10).
I'm using my own free xmlsq library as an example https://www.cryptosys.net/xmlsq.
You can download the core native DLL and all the source code quoted below. Make sure you use the 64-bit DLL.
The native Windows DLL diXmlsq.dll is written entirely in plain C code and exports simple C functions (extern "C").
In particular, for this example, it exports a XMLSQ_Gen_Version function that returns an integer value.
The DLL was compiled using MSVC 12.0 targetting the X64 platform. The associated library file generated by MSVC is diXmlsq.lib.
I should add that this DLL works exactly the same as a Windows "Win32 API" DLL, so the instructions here should work for the standard Windows libraries in Windows\System32 (again make sure you link against the 64-bit version).
Example 1. A plain C interface.
Both these commands compile without warning on my system:
> gcc -o test-ver test-ver.c "C:\fullpath\to\x64\diXmlsq.lib"
> gcc -o test-ver test-ver.c "C:\fullpath\to\x64\diXmlsq.dll"
diXmlsq.dll is compiled using the following definition file.
(You could alternatively use __declspec(dllexport))
Ref: https://learn.microsoft.com/en-us/cpp/build/exporting-from-a-dll?view=msvc-160
diXmlsq.def
LIBRARY "diXmlsq"
EXPORTS
XMLSQ_Gen_Version
diXmlsq.h - the C interface to diXmlsq.dll
#ifdef __cplusplus
extern "C" {
#endif
long __stdcall XMLSQ_Gen_Version(void);
#ifdef __cplusplus
}
#endif
To call the core function in a plain C program:
test-ver.c
#include <stdio.h>
#include "diXmlsq.h"
int main(void)
{
long n;
n = XMLSQ_Gen_Version();
printf("Version = %ld\n", n);
return 0;
}
Example 2. A C++ interface.
Both these commands compile without warning using g++ .
> g++ -o test-simple test-simple.cpp xmlsq.cpp "C:\fullpath\to\x64\diXmlsq.lib"
> g++ -o test-simple test-simple.cpp xmlsq.cpp "C:\fullpath\to\x64\diXmlsq.dll"
The idea of the C++ interface is to be an interface to the plain C library using the more convenient STL types like std::string and std::vector.
To keep things simple we'll just demonstrate the Gen::Version method.
Extracts of the C++ code follow:
test-simple.cpp - a test C++ program.
#include <iostream>
#include "xmlsq.hpp"
int main()
{
std::cout << "xmlsq::Gen::Version=" << xmlsq::Gen::Version() << std::endl;
}
xmlsq.hpp - the C++ interface
namespace xmlsq
{
class Gen {
private:
Gen() {} // Static methods only, so hide constructor.
public:
/** Get version number of core diXmlsq DLL. */
static int Version();
};
}
xmlsq.cpp - the C++ implementation.
#include "diXmlsq.h"
#include "xmlsq.hpp"
namespace xmlsq
{
int Gen::Version() {
int n = XMLSQ_Gen_Version();
return n;
}
}
Example 3. Attempting to link to the 32-bit library by mistake.
> gcc -o test-ver test-ver.c "C:\fullpath\to\Win32\diXmlsq.lib"
C:/Strawberry/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\user\AppData\Local\Temp\cce27Dhl.o:test-ver.c:(.text+0xe):
undefined reference to `XMLSQ_Gen_Version'
collect2.exe: error: ld returned 1 exit status

Unable to link libpng or zlib in Eclipse with MinGW C linker

I'm new to external static libraries in C, and i'm having trouble adding pnglib (or any library) to Eclipse. Im using Eclipse v3.3.2 with mingw on windows 7 64bit.
I first followed these instructions to install libpng and zlib: http://wiki.openttd.org/Compiling_on_MinGW
Then in Eclipse under C/C++ Build -> Settings ->Tool Settings -> MinGW C Linker -> Libraries
I added: "png" then "z" in Libraries (-l)
and: "C:\MinGW\libpng-1.5.12" then "C:\MinGW\zlib-1.2.7" in the Library search path (-L)
If I execute the simplest code:
#include <stdio.h>
#include <zlib.h>
#include <png.h>
int main(void) {
printf("foo\n");
unsigned char header[8];
//png_sig_cmp(header, 0, 0);
return 0;
}
It works fine, however as soon as i uncomment the function, the code compiles (without error/warning), but does absolutely nothing, (not even the print statement). This happens when I use ANY function from an external library.
I assume it can read the headers but there's funny business with finding function definitions.
I have no idea where i went wrong.
I'm sure I have missed something trivial!

Resources