Compling C++ code using command line - linux

A am using below command to compile my c++ code and which is using OpenCV libraries and my command is just like
opencv main.cpp -o binary_name
where opencv is an alias command like
alias opencv="g++ `pkg-config --cflags opencv` `pkg-config --libs opencv`"
but if I forget the "-o binary_name" the command delete my source file. Why this happening....?
What modification should I made on the above alias command to compile my source file like
opencv main.cpp binary_name
Thanks in advance.......

The order of arguments to gcc is important, source or object files should be given before libraries, and libraries should be invoked with higher level libraries before the lower level libraries they are using.
So you should compile with
g++ -Wall -g $(pkg-config --cflags opencv) main.cpp \
$(pkg-config --libs opencv) -o binaryprog
But you really should use a Makefile, or at least have a shellscript.
Don't forget the -Wall option to get all warnings. Improve your code till no warnings are given by the compiler. Use the -g option to get debugging information, to be able to use gdb ./binaryprog to debug your program.
Once your program is debugged, replace -g by -O3 (or perhaps by -O2 -g) to ask GCC to optimize the generated code.

You can use a function instead of an alias, and use arguments:
function opencv() { g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` "$1" -o "$2"; }

Related

Linking issue in Cross Compilation for arm in linux using aarch64-linux-gnu-

I got an error when try to link (-aarch64-linux-gnu-ld) (the script containing the Makefile was downloaded from https://github.com/Icenowy/aw-el2-barebone). Error is "aarch64-linux-gnu-ld: Error: unable to disambiguate: -nostartfiles (did you mean --nostartfiles ?)
make: *** [Makefile:31: el2-bb.elf] Error 1" How to recode the line 31? of the Makefile
CROSS_COMPILE = /usr/bin/aarch64-linux-gnu-
DEBUG = 1
CC = $(CROSS_COMPILE)gcc
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy
INCLUDES = -I ./include -I ./uboot-headers -ffreestanding
DEFINES = -DSOC_HEADER="\"h6.h\""
ifneq ($(DEBUG),1)
DEFINES += -DNDEBUG
endif
ASFLAGS = $(INCLUDES) $(DEFINES) -D__ASSEMBLY__
CFLAGS = $(INCLUDES) $(DEFINES) -O2
LDSCRIPT = ldscripts/a64.ld
LDSCRIPTS = ldscripts/a64.ld ldscripts/common.ld
LDFLAGS = -nostdlib -nostartfiles -static -T $(LDSCRIPT)
OBJS = start.o init.o uart.o stack.o exceptions.o exception_funcs.o panic.o pgtables.o trapped_funcs.o
all: el2-bb.bin
el2-bb.elf: $(OBJS) $(LDSCRIPTS)
$(LD) $(LDFLAGS) $(OBJS) -o $#
el2-bb.bin: el2-bb.elf
$(OBJCOPY) -O binary $< $#
.PHONY: clean
clean:
rm -f el2-bb.* *.o
-nostartfiles is a gcc option but not an ld option.
If you use gcc to invoke the linker (say with LD=gcc in your Makefile) for making a standalone program that does not use standard startup files, you should use -nostartfiles because gcc links the startup files by default and -nostartfiles disables this.
ld doesn't links any startup files by default, so there is no option to disable linking them. You always pass startup files explicitly to ld. If you don't want startup files, just don't pass them to ld.
In order to see what startup files on your system are create an empty C program:
int main(){}
and compile it:
gcc -c empty.c
gcc -v empty.o
You are likely to see an invocation of ld (or perhaps of collect2, which calls ld) with a long, long list of options and object files.
Now run
gcc -nostartfiles -v empty.o
The .o files are now gone. That's exactly what -nostartfiles does.
When you invoke ld empty.o, these files and options are not there to begin with. In order to make a working program for say a Linux system, you need to pass most of them to ld explicitly. If you are building a program for something other system, you may not need some or all of them. So just don't pass them in.

Makefile with MSVC and CLION

I use the ATL library so I have to use MSVC instead of MinGW. But with MSVC libraries written in Makefile are not seen (with Makefile they are). I suppose that MSVC doesn't see Makefile at all. Are there any supposes about how to compile the project?
# Remove NDEBUG define to trigger asserts
CPPFLAGS+=-O2 -std=gnu++11 -I. -DNDEBUG -Wall -Wno-sign-compare -Wno-unused -g -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DOPENSSL
LDFLAGS+=-levent -lstdc++ -lssl -lcrypto
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
ifeq ($(uname_S),FreeBSD)
CXX=clang++
LIBEVENT_HOME=D:/ProximaX/d/vcpkg/vcpkg/packages/libevent_x86-windows
CPPFLAGS+=-I${LIBEVENT_HOME}/include
LDFLAGS+=-L${LIBEVENT_HOME}/lib
else
CXX?=g++
LIBEVENT_HOME=D:/ProximaX/d/vcpkg/vcpkg/packages/libevent_x86-windows
CPPFLAGS+=-I${LIBEVENT_HOME}/include
LDFLAGS+=-L${LIBEVENT_HOME}/lib
OPENSSL_HOME=D:/ProximaX/d/vcpkg/vcpkg/packages/openssl_x86-windows
CPPFLAGS+=-I${OPENSSL_HOME}/include
LDFLAGS+=-L${OPENSSL_HOME}/lib
ATL_HOME=D:/VS17/VC/Tools/MSVC/14.16.27023/atlmfc
CPPFLAGS+=-I${ATL_HOME}/include
LDFLAGS+=-L${ATL_HOME}/lib
endif
all: swift-dynamic
swift: swift.o sha1.o compat.o sendrecv.o send_control.o hashtree.o bin.o binmap.o channel.o transfer.o httpgw.o statsgw.o cmdgw.o avgspeed.o avail.o storage.o zerostate.o zerohashtree.o livehashtree.o live.o api.o content.o swarmmanager.o address.o livesig.o exttrack.o
swift-static: swift
${CXX} ${CPPFLAGS} -o swift *.o ${LDFLAGS} -static -lrt
strip swift
touch swift-static
swift-dynamic: swift
${CXX} ${CPPFLAGS} -o swift *.o ${LDFLAGS}
touch swift-dynamic
clean:
rm -f *.o swift swift-static swift-dynamic 2>/dev/null
.PHONY: all clean swift swift-static swift-dynamic
Visual Studio does not use makefiles. You need to create a project and configure it correctly. See also this question.
That is why libraries that target both Windows and Linux provide both a makefile and a Visual Studio project file. Or they use a "meta build tool", such as CMake, that can generate both from the same source.
Alternatively you can rewrite the makefile to call the Visual Studio compiler but the compilation and linking options will be different.

Cliclock CC Build Error 'undefined reference' [duplicate]

I'm trying to compile my project and I use the lib ncurse. And I've got some errors when compiler links files.
Here is my flags line in Makefile:
-W -Wall -Werror -Wextra -lncurses
I've included ncurses.h
Some layouts :
prompt$> dpkg -S curses.h
libslang2-dev:amd64: /usr/include/slcurses.h
libncurses5-dev: /usr/include/ncurses.h
libncurses5-dev: /usr/include/curses.h
prompt$> dpkg -L libncurses5-dev | grep .so
/usr/lib/x86_64-linux-gnu/libncurses.so
/usr/lib/x86_64-linux-gnu/libcurses.so
/usr/lib/x86_64-linux-gnu/libmenu.so
/usr/lib/x86_64-linux-gnu/libform.so
/usr/lib/x86_64-linux-gnu/libpanel.s
And here are my erros :
gcc -W -Wall -Werror -Wextra -I./Includes/. -lncurses -o Sources/NCurses/ncurses_init.o -c Sources/NCurses/ncurses_init.c
./Sources/NCurses/ncurses_init.o: In function `ncruses_destroy':
ncurses_init.c:(.text+0x5): undefined reference to `endwin'
./Sources/NCurses/ncurses_init.o: In function `ncurses_write_line':
ncurses_init.c:(.text+0xc5): undefined reference to `mvwprintw'
./Sources/NCurses/ncurses_init.o: In function `ncurses_init':
ncurses_init.c:(.text+0xee): undefined reference to `initscr'
collect2: error: ld returned 1 exit status
Thanks a lot
You need to change your makefile so that the -lncurses directive comes after your object code on the gcc command line, i.e. it needs to generate the command:
gcc -W -Wall -Werror -Wextra -I./Includes/. -o Sources/NCurses/ncurses_init.o -c Sources/NCurses/ncurses_init.c -lncurses
This is because object files and libraries are linked in order in a single pass.
In C++ , I fixed it just by linking the ncurses library .
Here is the command :
g++ main.cpp -lncurses
I got flags to correct order by using LDLIBS variable:
ifndef PKG_CONFIG
PKG_CONFIG=pkg-config
endif
CFLAGS+=-std=c99 -pedantic -Wall
LDLIBS=$(shell $(PKG_CONFIG) --libs ncurses)
man gcc | grep -A10 "\-l library"
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX
compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files
in the order they are specified. Thus, foo.o -lz bar.o searches
library z after file foo.o but
before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

pkg-config --libs returns a .la file, but g++ can't handle it

I'm trying to build something against GStreamer from git. They have some magic process that makes pkg-config happy, but if I run it, I get a .la file:
$ pkg-config --libs gstreamer-1.0
/home/blong/gst/head/gstreamer/gst/libgstreamer-1.0.la -lgobject-2.0 -lglib-2.0
And that causes problems for GCC:
g++ -o example switchtrackexample.cpp `pkg-config --libs --cflags gstreamer-1.0`
# comes out as: g++ -o example switchtrackexample.cpp -pthread -I/home/blong/gst/head/gstreamer -I/home/blong/gst/head/gstreamer/libs -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include /home/blong/gst/head/gstreamer/gst/libgstreamer-1.0.la -lgobject-2.0 -lglib-2.0
/usr/bin/ld: error: /home/blong/gst/head/gstreamer/gst/libgstreamer-1.0.la:8:8: invalid character
/usr/bin/ld: error: /home/blong/gst/head/gstreamer/gst/libgstreamer-1.0.la:8:8: syntax error, unexpected $end
/usr/bin/ld: error: /home/blong/gst/head/gstreamer/gst/libgstreamer-1.0.la: not an object or archive
# lots of undefined references here
I've read some things suggesting I should be able to use libtool to compile this, but I can't figure out how. What am I supposed to do with this .la file?
It figures, right after I asked this, I found this answer by Julien Moutte:
Add :
libtool --mode=link
in front of your build command.
Following that advice, I get this, which works:
libtool --mode=link g++ -o example switchtrackexample.cpp `pkg-config --cflags --libs gstreamer-1.0`

Difference between -shared and -Wl,-shared of the GCC options

I know -Wl,-shared is a option of ld. I've seen some person compile like this,
$ gcc -shared -Wl,-soname,libtest.so -o libtest.so *.o
And some person like this
$ gcc -Wl,-shared -Wl,-soname,libtest.so -o libtest.so *.o
So, I want to know if there is some difference between -shared and -Wl,-shared.
Thanks.
There is a difference between passing -shared to gcc or -shared to ld (via -Wl). Passing -shared to GCC may enable or disable other flags at link time. In particular, different crt* files might be involved.
To get more information, grep for -shared in GCC's gcc/config/ directory and subdirectories.
Edit: To give a specific example: on i386 FreeBSD, gcc -shared will link in object file crtendS.o, while without -shared, it will link in crtend.o instead. Thus, -shared and -Wl,-shared are not equivalent.
I don't think there is any difference. -shared is not a supported option of gcc and it is passed to linker whether you specify it with -Wl or not. -Wl option of gcc is used to specify that a comma separated list of options is to be passed to linker for further processing.

Resources