5 Replies Latest reply: Apr 2, 2017 1:58 PM by stephenpankiw RSS

Trouble getting C code to run

stephenpankiw Newbie

I've been having trouble getting C code to run in my bb10 project

 

test.h

#ifndef TEST_H_
#define TEST_H_

#ifdef __cplusplus
extern "C" {
#endif

int callMe(void);

#ifdef __cplusplus
}
#endif

#endif /* TEST_H_ */

 

test.c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "test.h"

int callMe(void) {
    printf("C function called");
    return 10;
}

 

got that from this: thread as a test that *should* work

 

I'm getting "Syntax Error" for the extern "C" { line in  test.h

the compiler error is pretty vague:

make[2]: *** [o.le-v7-g/.obj/test.o] Error 1
make[1]: *** [debug] Error 2
make: *** [Device-Debug] Error 2

 

My compiler is also giving me this:

 

-Ic:/bbndk/target_10_2_0_1155/qnx6/usr/include/freetype2 -I. -o o.le-v7-g/.obj/test.o ../src/test.c
cc1: error: one or more PCH files were found, but they were invalid
cc1: error: use -Winvalid-pch for more information
cc1: fatal error: o.le-v7-g/.obj/BB_10: No such file or directory
compilation terminated.

 

BB_10 is the name of the project, and there's an empty BB_10 folder under JavaScript Resources on the Project Explorer with a red X icon on it. Don't know if this is at all related to add C files to the project, but the second I remove the C files, all the errors go away.

 

Is there an extra step, or something really simple that I'm missing to get C code to run? I'm completely stumped.

  • Re: Trouble getting C code to run
    uberschallsamsara Apprentice

    stephenpankiw wrote:

    When I clean then rebuild I go from 2 errors to over 300, and it's all fatal errors in common libraries like so:

     

    :/bbndk/target_10_2_0_1155/qnx6/usr/include/qt4/QtCore/qstring.h:54:20: fatal error: string: No such file or directory

     

    but qstring.h is in that folder, I don't know how the compiler is missing it

     

    or errors because it can't find a library anymore:

    c:/bbndk/target_10_2_0_1155/qnx6/usr/include/qt4/QtCore/qbytearray.h:606:1: error: unknown type name 'QByteArray'

     

    Can't find QByteArray type in qbytearray.h? What's going on?


     

    Regarding the first error, it is complaining about line 54 of qstring.h, not about not finding qstring.h.  At line 54 of qstring.h is:

     

    #  include <string>

     

    which is attempting to include a system lib.  A quick googling for "qstring.h:54:20: fatal error: string: No such file or directory" yields some forum discussions such as:

     

    http://qt-project.org/forums/viewthread/39367

     

    where one person apparently concluded that their use of .h/.c file extensions instead of .hpp/.cpp extensions was causing their build to not look for the system library in the correct place.  BTW I'm not snarking you by mentioning google searches, just trying to help and to learn something myself since i could well run into this issue myself.

    • Re: Trouble getting C code to run
      stephenpankiw Newbie

      That was exactly my issue, thanks!

       

      Rename test.h to a test.hpp and test.c to test.cpp and everything compiles fine and I can call those c functions with c++ code.

       

      I knew it was something dumb and obvious.

      • Re: Trouble getting C code to run
        jomurray Novice

        If you're interested in the root cause of this issue then read on :-)

         

        When you create a BlackBerry project in Momentics ( say Cascades ) the template that's used seems to populate the "precompiled.h" file in the root of the project with these "#includes":

         

        #include <QObject>
        #include <QString>
        #include <QVariant>

         

        The intent is that these headers should be pre-compiled, since they're ubiquitous in Cascacdes apps, as an efficiency measure.

         

        The qmake toolchain handles this in this way -- my project is called "TestLinkages" in what follows btw:

         

        1. In "/arm" it dynamically builds the proper makefiles (Makefile.Debug and Makefile.Release) for the project -- if there are only C++ (.cpp) source files in the project it adds this rule to these Makefiles -- I'm looking at Makefile.Debug here:
        o.le-v7-g/.obj/TestLinkages.gch/c++: ../precompiled.h 
         @$(CHK_DIR_EXISTS) o.le-v7-g/.obj/TestLinkages.gch/ || $(MKDIR) o.le-v7-g/.obj/TestLinkages.gch/ 
         $(CXX) $(CXXFLAGS) $(INCPATH) -x c++-header -c ../precompiled.h -o o.le-v7-g/.obj/TestLinkages.gch/c++

         This pre-compiles ("-x c++-header") precompiled.h using the C++ compiler and puts the binary into "/arm/o.le-v7-g/.obj/TestLinkages.gch/c++"

         

        2. These are included into the build later in the Makefile like this:

         

        o.le-v7-g/.obj/main.o: ../src/main.cpp \
          o.le-v7-g/.obj/TestLinkages.gch/c++
         $(CXX) -c -Wc,-include -Wc,o.le-v7-g/.obj/TestLinkages $(CXXFLAGS) $(INCPATH) -o o.le-v7-g/.obj/main.o ../src/main.cpp

         3. If only C++ source files are present in the project then this is fine and everything works.

         

        However, if there are any C source files (.c) in the project ( for example test.c/.h) then this additinal rule is added to the Makefiles in /arm by qmake:

         

        o.le-v7-g/.obj/TestLinkages.gch/c: ../precompiled.h 
         @$(CHK_DIR_EXISTS) o.le-v7-g/.obj/TestLinkages.gch/ || $(MKDIR) o.le-v7-g/.obj/TestLinkages.gch/ 
         $(CC) $(CFLAGS) $(INCPATH) -x c-header -c ../precompiled.h -o o.le-v7-g/.obj/TestLinkages.gch/c

         

        This uses the C compiler $(CC) to precompile ("-x c-header") the header file "precompiled.h" into the file "/arm/o.le-v7-g/.obj/TestLinkages.gch/c". The intent is to end up with two precompiled header binaries under /armo.le-v7-g/.obj/TestLinkages.gch" called "c" and "c++" for inclusion in the compile step later.

         

        However, when this rule in the Makefile is executed the C compiler tries to pre-compile "precompiled.h" which of course contains "#include" statements for C++ headers containing C++ class definintions etc. Of course the C compiler doesn't know what to do with these and you get pages of error messages.

         

        There are two ways to avoid this:

         

        1. Remove the "#include" lines for C++ headers in "precompiled.h" -- this is OK since precompilation is an efficiency measure and your project will build but may take a little longer.
        2. If you want to retain the benefit of pre-compiling "precompiled.h" then you could modify "precompiled.h" so that it looks like:
        #ifdef __cplusplus
        #include <QObject>
        #include <QString>
        #include <QVariant>
        #endif

         If the variable "__cplusplus" is defined then the file is part of a compilation unit being processed by the C++ compiler and the "#include" statements will be resolved. If the C compiler is used instead the "#include" lines will not be included.

  • Re: Trouble getting C code to run
    stephenpankiw Newbie

    When I clean then rebuild I go from 2 errors to over 300, and it's all fatal errors in common libraries like so:

     

    :/bbndk/target_10_2_0_1155/qnx6/usr/include/qt4/QtCore/qstring.h:54:20: fatal error: string: No such file or directory

     

    but qstring.h is in that folder, I don't know how the compiler is missing it

     

    or errors because it can't find a library anymore:

    c:/bbndk/target_10_2_0_1155/qnx6/usr/include/qt4/QtCore/qbytearray.h:606:1: error: unknown type name 'QByteArray'

     

    Can't find QByteArray type in qbytearray.h? What's going on?

  • Re: Trouble getting C code to run
    uberschallsamsara Apprentice

    Did you try cleaning your build folder, based on 

     

    " error: one or more PCH files were found, but they were invalid"

     

    ?