Linking with ‘g++’ causes "undefined reference to" a libray identified in the Makefile

Summary:

When I build my executable targets with ‘g++’ I get ‘undefined reference’ errors to libraries that were identifed in my Makefile and I have verified the order of.

Build System:

nVidia Jetson AGX Xavier (aarch64)

Makefile:

#Compiler/Linker
CC=g++

#External Dependcies
PYX_INC = /opt/pyxis/include
PYX_LIB = /opt/pyxis/lib64
EBUS_LIB = /opt/pleora/ebus_sdk/linux-aarch64-arm/lib
CV_LIB = /opt/opencv/lib

#Compiler Flags
STD_FLAGS = -std=c++11
WAE_FLAGS = -Wall
DEBUG_FLAGS = -g3
OPT_FLAGS = -O2
CPPFLAGS = $(STD_FLAGS) $(WAE_FLAGS) -I include
BIN_CCFLAGS = $(CPPFLAGS)
LIB_CCFLAGS = $(CPPFLAGS) -c -I $(PYX_INC)

#Linker Flags
LD_SEARCH = -L $(EBUS_LIB) -L $(PYX_LIB) -L $(CV_LIB) -L lib
CV_LIBS = -l opencv_core -l opencv_imgproc -l opencv_imgcodecs -l opencv_highgui
PV_LIBS = -l PvBase -l PvDevice -l PvStream
PAHO_LIBS = -l paho-mqtt3a -l paho-mqtt3as -l paho-mqtt3c -l paho-mqtt3cs -l paho-mqttpp3 
LDFLAGS = $(LD_SEARCH) $(PV_LIBS) $(PAHO_LIBS) $(CV_LIBS) -l PyxisLWIR -l pyxiscollector

#File Listings
LIB_CPP = src/Archive.cpp src/PyxisError.cpp src/Transport.cpp src/PahoTransport.cpp src/Frame.cpp src/Collector.cpp src/FileCollector.cpp src/CameraCollector.cpp
LIB_OBJ = build/Archive.o build/PyxisError.o build/Transport.o build/PahoTransport.o build/Frame.o build/Collector.o build/FileCollector.o build/CameraCollector.o
LIB_HPP = include/Archive.hpp include/PyxisError.hpp include/Transport.hpp include/PahoTransport.hpp include/Frame.hpp include/Collector.hpp include/FileCollector.hpp include/CameraCollector.hpp
LIB_OUT = lib/libpyxiscollector.a

#Rules Begin
#-----------------
default: all

all: run_file run_camera
    @echo "Build Complete"

#Run FileCollector binary
run_file: static src/run_file.cpp
    $(CC) -o bin/run_file src/run_file.cpp $(BIN_CCFLAGS) $(LDFLAGS)

#Run CameraCollector Binary
run_camera: static src/run_camera.cpp
    $(CC) -o bin/run_camera src/run_camera.cpp $(BIN_CCFLAGS) $(LDFLAGS)

#Build Library
build/PyxisError.o: src/PyxisError.cpp include/PyxisError.hpp
    $(CC) $(LIB_CCFLAGS) src/PyxisError.cpp -o [email protected]
build/Archive.o: src/Archive.cpp include/Archive.hpp
    $(CC) $(LIB_CCFLAGS) src/Archive.cpp -o [email protected]
build/Transport.o: src/Transport.cpp include/Transport.hpp
    $(CC) $(LIB_CCFLAGS) src/Transport.cpp -o [email protected]
build/PahoTransport.o: src/PahoTransport.cpp include/PahoTransport.hpp
    $(CC) $(LIB_CCFLAGS) src/PahoTransport.cpp -o [email protected]
build/Frame.o: src/Frame.cpp include/Frame.hpp
    $(CC) $(LIB_CCFLAGS) src/Frame.cpp -o [email protected]
build/Collector.o: src/Collector.cpp include/Collector.hpp
    $(CC) $(LIB_CCFLAGS) src/Collector.cpp -o [email protected]
build/FileCollector.o: src/FileCollector.cpp include/FileCollector.hpp
    $(CC) $(LIB_CCFLAGS) src/FileCollector.cpp -o [email protected]
build/CameraCollector.o: src/CameraCollector.cpp include/CameraCollector.hpp
    $(CC) $(LIB_CCFLAGS) src/CameraCollector.cpp -o [email protected]

#Create Static Library
static: $(LIB_OBJ)
    @ar crs $(LIB_OUT) $^ 

#Clean Project
clean:
    @rm -f build/*
    @echo "Cleaned Targets"

OUTPUT:

[email protected]:/opt/cuas/cuas-collector$ make
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/Archive.cpp -o build/Archive.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/PyxisError.cpp -o build/PyxisError.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/Transport.cpp -o build/Transport.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/PahoTransport.cpp -o build/PahoTransport.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/Frame.cpp -o build/Frame.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/Collector.cpp -o build/Collector.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/FileCollector.cpp -o build/FileCollector.o
g++ -std=c++11 -Wall -I include -c -I /opt/pyxis/include src/CameraCollector.cpp -o build/CameraCollector.o
g++ -o bin/run_file src/run_file.cpp -std=c++11 -Wall -I include -L /opt/pleora/ebus_sdk/linux-aarch64-arm/lib -L /opt/pyxis/lib64 -L /opt/opencv/lib -L lib -l 
PyxisLWIR -l PvBase -l PvDevice -l PvStream -l paho-mqtt3a -l paho-mqtt3as -l paho-mqtt3c -l paho-mqtt3cs -l paho-mqttpp3  -l opencv_core -l opencv_imgproc -l opencv_imgcodecs -l opencv_highgui  -l pyxiscollector
lib/libpyxiscollector.a(PahoTransport.o): In function `cuas::PahoTransport::PahoTransport()':
PahoTransport.cpp:(.text+0x268): undefined reference to `mqtt::async_client::async_client(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, mqtt::iclient_persistence*)'
lib/libpyxiscollector.a(PahoTransport.o): In function `cuas::PahoTransport::send(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, cv::Mat&)':
PahoTransport.cpp:(.text+0x65c): undefined reference to `mqtt::topic::publish(void const*, unsigned long, int, bool)'
lib/libpyxiscollector.a(Collector.o): In function `cuas::Collector::Collector(cuas::Transport&)':
Collector.cpp:(.text+0x104): undefined reference to `Pyxis::PyxisLWIR::PyxisLWIR(bool)'
lib/libpyxiscollector.a(Collector.o): In function `cuas::Collector::Collector(cuas::Transport&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Collector.cpp:(.text+0x198): undefined reference to `Pyxis::PyxisLWIR::PyxisLWIR(bool)'
Collector.cpp:(.text+0x1fc): undefined reference to `Pyxis::PyxisLWIR::~PyxisLWIR()'
lib/libpyxiscollector.a(Collector.o): In function `cuas::Collector::calibrate()':
Collector.cpp:(.text+0x2bc): undefined reference to `Pyxis::PyxisLWIR::LoadNucFile(char const*, char const*)'
lib/libpyxiscollector.a(Collector.o): In function `cuas::Collector::run()':
Collector.cpp:(.text+0x5ec): undefined reference to `cv::waitKey(int)'
lib/libpyxiscollector.a(FileCollector.o): In function `cuas::Collector::~Collector()':
FileCollector.cpp:(.text._ZN4cuas9CollectorD2Ev[_ZN4cuas9CollectorD5Ev]+0x3c): undefined reference to `Pyxis::PyxisLWIR::~PyxisLWIR()'
lib/libpyxiscollector.a(Frame.o): In function `cuas::Frame::compute(Pyxis::PyxisLWIR&)':
Frame.cpp:(.text+0x1bc): undefined reference to `Pyxis::PyxisLWIR::ComputePol(cv::Mat*, cv::Mat*, cv::Mat*, cv::Mat*, cv::Mat*, cv::Mat*)'
lib/libpyxiscollector.a(Frame.o): In function `cuas::Frame::makeFrame(Pyxis::PyxisLWIR&)':
Frame.cpp:(.text+0x278): undefined reference to `Pyxis::PyxisLWIR::GetFrame(cv::Mat*, bool, int)'
collect2: error: ld returned 1 exit status
Makefile:41: recipe for target 'run_file' failed
make: *** [run_file] Error 1

PahoTransport.cpp: (for first undefined reference)

#include <string>
#include <cstdint>
#include <mqtt/async_client.h>
#include "PahoTransport.hpp"

namespace cuas
{
    PahoTransport::PahoTransport()
    {
        std::string host = "localhost:1883";
        std::string clientId = "pyxis_collector";


        ptrClient.reset( new mqtt::async_client( host, clientId, nullptr ) );
        ptrClient->connect()->wait();
    }

    PahoTransport::~PahoTransport()
    {
    }

    std::shared_ptr<mqtt::topic> PahoTransport::getTopic( std::string topicName )
    {
        std::shared_ptr<mqtt::topic> rVal;

        auto it = topics.find( topicName );

        if( it != topics.end() )
        {
            rVal = it->second;
        }
        else
        {
            rVal.reset( new mqtt::topic( *ptrClient, topicName, MQTT_QOS, false ) );

            topics.insert( { topicName, rVal } );
        }

        return rVal;
    }

    void PahoTransport::send( std::string channelName, uint32_t index, cv::Mat& mat )
    {
        getTopic( channelName )->publish( reinterpret_cast<void*>( archive.serialize( index, mat ) ), size_t(archive.getBufferSize()), MQTT_QOS, false );
    }
}

Thanks so much for any help you can provide, I appreciate it.

Source: Windows Questions C++

LEAVE A COMMENT