Home > OS >  Make: How to write makefile with both hpp and cpp file mixture project
Make: How to write makefile with both hpp and cpp file mixture project

Time:08-03

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

CodePudding user response:

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.

  • Related