1

I'm using ceedling + Cmock to unit test my methods. I've been following this guide. In my project, I have a foo.h and foo.c file:

foo.c:

#include <nfc/nfc.h>
#include <stdbool.h>
#include "foo.h"

void do_something_with_nfc() {
   //... other code

   // how might I mock this call?  It is a method of nfc.h
   bool result = nfc_initiator_transceive_bytes(pnd, msg, msg_len, buffer, buffer_len, 0);
}

In my test_foo.c file:

#include "mock_nfc.h" // should this include be <mock_nfc.h>, <nfc/mock_nfc.h> or "nfc/mock_nfc.h"?
#include "foo.h"

void test_do_something_with_nfc() {
    // mocked behavior
    nfc_initiator_transceive_bytes_IgnoreAndReturn(-1);

    do_something_with_nfc();
}

PROBLEM: The error that I'm getting when I run "ceedling" is:

ERROR: Found no file 'nfc.h' in search paths.

libnfc is a library that I installed and is located at /usr/local/lib/libnfc (archive file). I'm extremely new to C development, but I figured CMock needs to be able to find the nfc.h header file in order to generate the mock. If I were simply compiling my project, I would add -lnfc so my next step was to figure out how ceedling/Cmock needed this to be conveyed. To that end, I looked up ways to specify paths through the ceedling project.yml file:

:paths
  :libraries: [/usr/local/lib, /usr/lib, /usr/local/lib/libnfc.a]

Even with the above changes, I still receive an "nfc.h" not found error. Is it more customary to download the referenced library's source (libnfc in this case), and add that source's path to the ceedling project.yml file? Thanks for the assistance.

Progman
  • 16,827
  • 6
  • 33
  • 48
Tung
  • 5,334
  • 1
  • 34
  • 41
  • The library paths will only be important for the link phase but the header is missing during *compilation.* (That's a confusion you share with many beginners because linking with the standard library is automatic and hence invisible, so missing a header is the only thing that can go wrong.) In any event, what you need to change is a similar entry regarding include paths. (Perhaps SamL's answer is on the right track.) – Peter - Reinstate Monica Apr 06 '22 at 22:09

1 Answers1

1

Add following to YML file, if you need to include the path and see it works

:files:
      :include:
        - +:/usr/lib/**

:paths:
  :test:
    - +:test/**
  :source:
    - src/**
    - test/support/**

  :include:
    - include/**   
  :support:
  :libraries: []
:files:
  :include:
    - +:/usr/lib/**

If you are trying to test symbolic funtions try follwing way instread which worked for me. How to mock socket in C

SamL
  • 37
  • 1
  • 10
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 00:09
  • @SamL, I appreciate the effort. Although libnfc was installed in /usr/local/lib, I had to check out the source code, and then copy the header files into a location that I then added to :paths:include of the .yml. I awarded you a +1 for the effort, and your solution is on the path to being correct *if* the third party headers were in an "include" subdirectory. – Tung Apr 18 '22 at 06:11