0

I am trying to make a makefile for my project which makes out of a Main.cpp file which includes some *.hpp/h files (calling it L.hpp,D.h,UC.hpp). The header files(mainly the hpp) contain both implementation and declaration. In addition, Main.cpp and L.hpp uses a class object C. Below is a diagram of its relationship.

Relationship between files

After which I proceed to write a makefile for it (as below). The common variables eg CXX are left out (as it is written in an environment script and I just source the file before running the makefile). The additional variables ($(MISC), $(ADD_INC), $(LIB_LOC) $(LDLIBS)) are various includes, libraries, compilation settings that main.cpp and L.hpp requires

###
### Library file 
###
LDLIBS  =  -lxml2  -ldl -lgstpbutils-1.0 -lgstsdp-1.0  -lgstapp-1.0 -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0  -lpthread -lstdc++ -lnsl -lm
LIB_PKG    = -L${SDKTARGETSYSROOT}/usr/lib/pkgconfig
LIB_GST    = -L${SDKTARGETSYSROOT}/usr/lib/gstreamer
LIB_PRJ    = -L${SDKTARGETSYSROOT}/usr/lib

LIB_LOC    = $(LIB_PKG) $(LIB_GST) $(LIB_PRJ)

###
### Header file dependency list
###
# include for gstreamer
INC_GST      = -I$(SDKTARGETSYSROOT)/usr/include/gstreamer-1.0/

# include for glib
INC_GLIB         = -I$(SDKTARGETSYSROOT)/usr/include/glib-2.0
INC_GLIB_CONFIG  = -I$(SDKTARGETSYSROOT)/usr/lib/glib-2.0/include
      
ADD_INC          = $(INC_GST) $(INC_GLIB) $(INC_GLIB_CONFIG) 

MISC             = -std=c++11 -c -fpermissive -fmessage-length=0 $(shell pkg-config --cflags)


# partial makefile
#
# Define the Target name and main object list
#
FINAL_TARGET = VS2
OBJ          = ../Main.cpp C.o    

# 
# Build Rules
#
all: $(FINAL_TARGET)

C.o: ../C.cpp ../C.h
    $(CXX) $(CXXFLAGS) $(MISC) -c $< -o $@ $(LDFLAGS) $(LIB_LOC) $(LDLIBS)


$(FINAL_TARGET): $(OBJ) 
    $(CXX) $(CXXFLAGS) $(ADD_INC) $(MISC) -o $(FINAL_TARGET) $(OBJ)  $(LDFLAGS) $(LIB_LOC) $(LDLIBS)

clean:
    rm -rf  $(FINAL_TARGET) *.o

The compilation, apart from getting C.o: linker input file unused because linking not done as a warning completed without any other problem. However, the end product of the VS2 seem not to be running properly when transplanted to the environment and run. I have used file VS2 and found that VS2 is actually a relocatable files (seem like linking not done)

So I have the following question:

  • Am I writing the makefile correctly? by only compiling for main.cpp and C.cpp

  • How should i include hpp file? Have read the Write makefile for .cpp and .hpp c++. Can't say I fully understand the author but it seem to call for putting in lines as

    L.o: ../L.hpp OBJ = ../Main.cpp C.o L.o

Need your advice

==================================================================

EDIT For more information

Basically I just source the file below before I make using the makefile above

# Check for LD_LIBRARY_PATH being set, which can break SDK and generally is a bad practice
# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80
# http://xahlee.info/UnixResource_dir/_/ldpath.html
# Only disable this check if you are absolutely know what you are doing!
if [ ! -z "$LD_LIBRARY_PATH" ]; then
    echo "Your environment is misconfigured, you probably need to 'unset LD_LIBRARY_PATH'"
    echo "but please check why this was set in the first place and that it's safe to unset."
    echo "The SDK will not operate correctly in most cases when LD_LIBRARY_PATH is set."
    echo "For more references see:"
    echo "  http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80"
    echo "  http://xahlee.info/UnixResource_dir/_/ldpath.html"
    return 1
fi

export SDKTARGETSYSROOT=/opt/wayland/sysroots/cortexa53-crypto-poky-linux
export PATH=/opt/wayland/sysroots/x86_64-pokysdk-linux/usr/bin:/opt/wayland/sysroots/x86_64-pokysdk-linux/usr/sbin:/opt/wayland/sysroots/x86_64-pokysdk-linux/bin:/opt/wayland/sysroots/x86_64-pokysdk-linux/sbin:/opt/wayland/sysroots/x86_64-pokysdk-linux/usr/bin/../x86_64-pokysdk-linux/bin:/opt/wayland/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux:/opt/wayland/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux-musl:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH=$SDKTARGETSYSROOT/usr/lib/pkgconfig:$SDKTARGETSYSROOT/usr/share/pkgconfig
export CONFIG_SITE=/opt/wayland/site-config-cortexa53-crypto-poky-linux
export OECORE_NATIVE_SYSROOT="/opt/wayland/sysroots/x86_64-pokysdk-linux"
export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"
export OECORE_ACLOCAL_OPTS="-I /opt/wayland/sysroots/x86_64-pokysdk-linux/usr/share/aclocal"
export OECORE_BASELIB="lib"
export OECORE_TARGET_ARCH="aarch64"
export OECORE_TARGET_OS="linux"
unset command_not_found_handle
export CC="aarch64-poky-linux-gcc  -mcpu=cortex-a53 -march=armv8-a+crc+crypto -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export CXX="aarch64-poky-linux-g++  -mcpu=cortex-a53 -march=armv8-a+crc+crypto -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export CPP="aarch64-poky-linux-gcc -E  -mcpu=cortex-a53 -march=armv8-a+crc+crypto -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export AS="aarch64-poky-linux-as "
export LD="aarch64-poky-linux-ld  --sysroot=$SDKTARGETSYSROOT"
export GDB=aarch64-poky-linux-gdb
export STRIP=aarch64-poky-linux-strip
export RANLIB=aarch64-poky-linux-ranlib
export OBJCOPY=aarch64-poky-linux-objcopy
export OBJDUMP=aarch64-poky-linux-objdump
export READELF=aarch64-poky-linux-readelf
export AR=aarch64-poky-linux-ar
export NM=aarch64-poky-linux-nm
export M4=m4
export TARGET_PREFIX=aarch64-poky-linux-
export CONFIGURE_FLAGS="--target=aarch64-poky-linux --host=aarch64-poky-linux --build=x86_64-linux --with-libtool-sysroot=$SDKTARGETSYSROOT"
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now"
export CPPFLAGS=""
export KCFLAGS="--sysroot=$SDKTARGETSYSROOT"
export OECORE_DISTRO_VERSION="3.3.0"
export OECORE_SDK_VERSION="3.3.0"
export ARCH=arm64
export CROSS_COMPILE=aarch64-poky-linux-

# Append environment subscripts
if [ -d "$OECORE_TARGET_SYSROOT/environment-setup.d" ]; then
    for envfile in $OECORE_TARGET_SYSROOT/environment-setup.d/*.sh; do
        . $envfile
    done
fi
if [ -d "$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
    for envfile in $OECORE_NATIVE_SYSROOT/environment-setup.d/*.sh; do
        . $envfile
    done
fi

=================================================================

Edit for @n. 1.8e9-where's-my-share m. [CORRECTED]

After make, I have noticed this message as part of output on terminal

C.o: linker input file unused because linking not done

And copying VS2 to the environment, I execute the file VS2

root@z-mx8mm:/usr/local# ./VS2
-sh: ./VS2: cannot execute binary file: Exec format error
root@z-mx8mmt:/usr/local# 

At first I have thought of maybe that the file permission is not set properly

root@z-mx8mmt:/usr/local# chmod +x VS2

But the above error still remains. Then I use file command

root@z-mx8mmt:/usr/local# file VS2
VS2: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), with debug_info, not stripped

That's how i derive the conclusion

user1538798
  • 1,075
  • 3
  • 17
  • 42
  • 1
    The problems you're having have absolutely nothing to do with what header files are included by which source files, and are 100% related to the arguments you're passing to your compile command and your link command. But, you've not provided any of the values of these variables nor have you shown us (via cut and paste please, not screenshot) the compile or link lines that make is invoking to build your program. So, there's little we can do to help you. – MadScientist Aug 02 '22 at 03:19
  • Please provide a complete [mre]. That is, at least all the missing variables that you haven't already shown. And also show basic troubleshooting results such as a the relevant parts of the build log, especially the exact command that the warning came from. – kaylum Aug 02 '22 at 04:03
  • @MadScientist: Do you mean the $(CXX) etc etc has problem?. Basically I am trying to cross compile and I just source the env file provided (as above) and use the same shell to make (also provided above) – user1538798 Aug 02 '22 at 04:35
  • Please post any error messages you are getting verbatim. – n. m. could be an AI Aug 02 '22 at 04:41
  • @n. 1.8e9-where's-my-share m. : No error message whatsoever. It is just a warning message – user1538798 Aug 02 '22 at 04:43
  • So how do you know that it doesn't work? – n. m. could be an AI Aug 02 '22 at 05:02
  • @n. 1.8e9-where's-my-share m.: I check it is unable to execute when i ported the file to the environment and tried to run it. Then I then use command file... it tells me I have a relocatable file... which is a step before linking... MadScientist also believe the problem lies with my parameter for compiling – user1538798 Aug 02 '22 at 05:14
  • "it is unable to execute" **How do you know?** Please show what you observe, verbatim, not what you conclude from what you observe. You type some commands, you observe some output (or no output at all). What do you observe exactly? Copy the screen (as text). "it tells me I have a relocatable file" Copy and paste what it tells you, don't reformulate in your own words. – n. m. could be an AI Aug 02 '22 at 05:20
  • @n. 1.8e9-where's-my-share m. As in the edit – user1538798 Aug 02 '22 at 05:32
  • There is no file named VST mentioned in your makefile or scripts. Where did you get that file? – n. m. could be an AI Aug 02 '22 at 06:13
  • @n. 1.8e9-where's-my-share m. Sorry edited wrongly. Corrected them – user1538798 Aug 02 '22 at 06:20

1 Answers1

2

Exhibit A:

    $(FINAL_TARGET): $(OBJ) 
        $(CXX) $(CXXFLAGS) $(ADD_INC) $(MISC) ....

Exhibit B:

    MISC             = -std=c++11 -c ...

-c produces an object file, not an executable. You don't need any of these MISC flags when linking.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Thanks it really does work though I have to solve the code portion that failed without -fpermissive. Thanks for your help again – user1538798 Aug 02 '22 at 09:12