Unable to build project that includes JetsonGPIO using cmake

Hi All!

I’m working on a simple PID control program for my Jetson Nano 2GB. Using the JetsonGPIO library for C++ from this link, I was able to control my motor using PWM by writing my own makefile and compiling the program.

However, I really want to be able to build and run this project in CLion, which only uses cmake as far as I know. The problem is that I simply can’t get cmake to compile the GPIO library. My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.10)

find_package(Threads REQUIRED)
add_compile_options(-pthread -lJetsonGPIO)


add_executable(Motor main.cpp PID/PID.cpp PID/PID.h)

(The reason I really want to use CLion is so that I can debug and step through my code easily).

Any help would be greatly appreciated.

hello fouriearmandt ,

may I know what’s the compile error shows?
I’ve never use CLion before, however, Jetson.GPIO is a python library for 40 pin header configuration. for an alternative ways, you may use some general APIs to access the pin.

Hi Jerry. The error looks like this:

[ 33%] Building CXX object CMakeFiles/Motor.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/Motor.dir/PID/PID.cpp.o
[100%] Linking CXX executable Motor
CMakeFiles/Motor.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x250): undefined reference to `GPIO::setwarnings(bool)'
main.cpp:(.text+0x274): undefined reference to `GPIO::cleanup(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x340): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
main.cpp:(.text+0x350): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
main.cpp:(.text+0x36c): undefined reference to `GPIO::add_event_detect(int, GPIO::Edge, void (*)(int), unsigned long)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::PID()':
PID.cpp:(.text+0xf0): undefined reference to `GPIO::PWM::PWM(int, int)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::setupPins(bool)':
PID.cpp:(.text+0x1b0): undefined reference to `GPIO::setmode(GPIO::NumberingModes)'
PID.cpp:(.text+0x1c4): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
PID.cpp:(.text+0x1d8): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
PID.cpp:(.text+0x1ec): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
PID.cpp:(.text+0x200): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
PID.cpp:(.text+0x21c): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x22c): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x240): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x250): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x260): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x274): undefined reference to `GPIO::setup(int, GPIO::Directions, int)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::ChangeDutyCycle(float)':
PID.cpp:(.text+0x38c): undefined reference to `GPIO::PWM::ChangeDutyCycle(double)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::setDirection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
PID.cpp:(.text+0x5b0): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x5c0): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x5f4): undefined reference to `GPIO::output(int, int)'
PID.cpp:(.text+0x604): undefined reference to `GPIO::output(int, int)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::start(float)':
PID.cpp:(.text+0x6e0): undefined reference to `GPIO::PWM::start(double)'
CMakeFiles/Motor.dir/PID/PID.cpp.o: In function `PID::~PID()':
PID.cpp:(.text+0x1210): undefined reference to `GPIO::cleanup(int)'
PID.cpp:(.text+0x121c): undefined reference to `GPIO::cleanup(int)'
PID.cpp:(.text+0x1228): undefined reference to `GPIO::cleanup(int)'
PID.cpp:(.text+0x1234): undefined reference to `GPIO::cleanup(int)'
PID.cpp:(.text+0x1240): undefined reference to `GPIO::cleanup(int)'
collect2: error: ld returned 1 exit status
CMakeFiles/Motor.dir/build.make:120: recipe for target 'Motor' failed
make[2]: *** [Motor] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Motor.dir/all' failed
make[1]: *** [CMakeFiles/Motor.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

This gets generated every time I try to compile the program using CMake. I can compile it just fine with g++ by simply adding the -lJetsonGPIO flag, but for some reason CMake just can’t deal with it. Also I’m aware the JetsonGPIO also exists in python, but I need to use C++ so that version of the library is not an option for me.

hello fouriearmandt,

there’re lots of error report undefined reference, this should due to mismatch header file for the function definition.

Hi Jerry. That’s the problem, yes. I have built and installed the JetsonGPIO library from source. I am able to compile my porgram by writing a simple makefile and compiling with g++. In my main.cpp, all I have to do is add #include <JetsonGPIO.h> and add the -IJetsonGPIO flag when compiling. When using cmake, I get the errors shown above.

As we updated that we never used CLion, don’t know how to get it work.
May other developers help to share experienecs.


Eventually I figured out how to handle it, though the solution felt really messy. I ended up adding all of the JetsonGPIO files to the project itself and compiling it that way.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.