2

I have a rather large C program that I have already written. I'm now being asked to utilize a C++ class that a co-worker has written. I'm use to working with python, so writing my application in C and now being asked to work with C++ is causing some migraines. So ignoring my main application I decided to do some research and came across a youtube video that shows me how to do a very simple C program that calls a C++ class.

In order to compile the C program and allow it to call a C++ file, I needed to do two things, 1) use the C++ compiler (g++) and make sure to surround my c++ functions with < extern "C">. By doing this I was able to compile and run the application with no issues.

Here are the files that I'm currently using.

cppObject.cpp
cppObject.h
cppFunctions.cpp
cppFunctions.h
test.c

"test.c" is where the main method is, which calls the cppFunctions class, which then references functions within cppObject.cpp. Now I don't think I need to go into each of these functions as I can compile this with no issues, using.

g++ cppObject.cpp cppFunctions.cpp test.c -o test_exc

This generates an executable which runs with no issues.

Now because this is being built with Petalinux, Petalinux generates a recipe-app that includes a bitbake file and a makefile. Which both need to be modified in some way to accomplish my goal.

I'll be using this test case to reference for my main application.

As my C application has mainly been written, compiles, and runs with no issues. I decided to model the makefile and bitbake files after them.

APP = APP_name

# Add any other object files to this list below
APP_OBJS = file1.o \
file2.o \
file3.o \
file4.o \
file5.o \
# ...

all: build

build: $(APP)

LDLIBS += -lm
LDFLAGS += -lpthread

$(APP): $(APP_OBJS)
    $(CC) -o $@ $(APP_OBJS) $(LDFLAGS) $(LDLIBS)
clean:
    rm -f $(APP) *.o

So based on the above makefile I attempted the same thing, using the test applications file names, and g++ instead of gcc

APP = test

# Add any other object files to this list below
APP_OBJS = cppObject.o cppFunctions.o test.o

all: build

build: $(APP)

$(APP): $(APP_OBJS)
    $(CXX) -o $@ $(APP_OBJS) $(LDFLAGS) $(LDLIBS)
clean:
    rm -f $(APP) *.o

Now the bitbake file which matches my original application looks like this.

#
# This file is the **** recipe.
#

SUMMARY = "Simple **** application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI =   "file://file1.c \
            file://file2.c \
            file://file3.c \
            file://file4.c \
            file://file5.c \
            file://file2.h \
            file://file3.h \
            ...          
            "

S = "${WORKDIR}"

do_compile() {
         oe_runmake
}

do_install() {
         install -d ${D}${bindir}
         install -m 0755 **** ${D}${bindir}
}

I ended up making this one for the test application

#
# This file is the test recipe.
#

SUMMARY = "Simple test application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://cppObject.cpp \
       file://cppFunctions.cpp \
       file://cppObject.h \
       file://cppFunctions.cpp \
       file://test.c \
       file://Makefile \
          "

S = "${WORKDIR}"

do_compile() {
         oe_runmake
}

do_install() {
         install -d ${D}${bindir}
         install -m 0755 test ${D}${bindir}
}

The petalinux-build -c for the application fails to build and the main error "test.c:2:10: fatal error: cppFunctions.h No such file or directory"

If I setup the makefile on its own and run make, it will build just fine, if I replace the objectfiles with the C/C++ files instead.

I'm assuming what is happening is Petalinux uses the bitbake file to generate the object files. Which then the Makefile uses to build the actual application.

Any insight that would allow me to use petalinux to build the application.


So caught a error in the .bb file, @vermaete also pointed it out. I left off the cppFunctions.h file. I ended up adding it to the .bb file and rebuilt the recipe. Unfortunately, I'm still getting a build error. However, its different, in that its having issues with an undefined reference.

undefined reference to 'printObjValue'

Which is a function call.

Michael
  • 174
  • 2
  • 13
  • 1
    It is a lot easier to put your source code in git, and let petalinux/yocto clone the repo. But is you would us it as it is, you have to add all source code (headers and makefile too) to the SRC_URI. – vermaete Jun 25 '20 at 19:44
  • @vermaete, I'm really sorry. I'm not sure I follow all of what your trying to explain. It seems, that there may be a way to have a git repo with my source code that will allow Petalinux to pull from and build off of that. Ok, that sounds reasonable. I'll dig into that as a possible option. The second half of your comment I'm a little off on. Can you elaborate? – Michael Jun 25 '20 at 22:07
  • you should add cppFunctions.h to the SRC_URI – vermaete Jun 26 '20 at 04:39
  • @vermaete o yea, I caught that and tested it as well. But I'm still getting an error that I can't resolve. – Michael Jun 26 '20 at 14:19

0 Answers0