void *realloc(void *ptr, size_t size) Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main () {
int n=4;//number of elements
int *a;
a = (int*)calloc(n, sizeof(int));
char *str;
str = (char *) malloc(15);
strcpy(str, "tutorialspoint");
str = (char *) realloc(str, 25);
strcat(str, ".com");//append string
free(str);
free( a );
return(0);
}
build-essential : tools and libraries that are required to compile a program. For example, if you need to work on a C/C++ compiler, you need to install essential meta-packages on your system before starting the C compiler installation. When installing the build-essential packages, some other packages such as G++, dpkg-dev, GCC and make, etc. also install on your system.
Cmake + Ninja
GDB: The GNU Project Debugger
then for Qt and vcpkg
Besides build-essential and cmake you need to install git
development tools you have to install
sudo apt-get install build-essential curl zip unzip tar pkg-config gperf bison git autopoint gettext libtool
Vcpkg helps you manage C and C++ libraries on Windows, Linux and MacOS. This tool and ecosystem are constantly evolving, and we always appreciate contributions!
//for windows
> git clone https://github.com/microsoft/vcpkg
> .\vcpkg\bootstrap-vcpkg.bat
> set PATH=vcpkg_folder;%PATH%
//for linux you need g++
$ sudo apt-get update
$ sudo apt-get install build-essential tar curl zip unzip
$ sudo apt-get install -y pkg-config
$ git clone https://github.com/microsoft/vcpkg
$ ./vcpkg/bootstrap-vcpkg.sh
Search , Install and remove packages
.\vcpkg\vcpkg search [search term]
.\vcpkg\vcpkg install [packages to install]
.\vcpkg\vcpkg install [package name]:x64-windows
.\vcpkg\vcpkg install [packages to install] --triplet=x64-windows
.\vcpkg\vcpkg remove [packages to install]
In order to use vcpkg with Visual Studio, run the following command (may require administrator elevation):
gcc and g++ are the GNU C and C++ compilers, respectively
clang and clang++ are the LLVM compilers, respectively
Use CMake to define how to invoke compilers on the source code files
gdb/lldb — debuggers
gdb is a debugger provided by GNU
lldb is a debugger provided by LLVM
Use these to detect issues when running your binaries
It generally does not matter which compiler was used to generate the binary, i.e. you can use LLDB to debug a code compiled with a GNU compiler or vice versa
diffrence between make and cmake
Make (or rather a Makefile) is a buildsystem – it drives the compiler and other build tools to build your code.
CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions. From the same starting point, the same CMakeLists.txt file. So if you have a platform-independent project, CMake is a way to make it buildsystem-independent as well.
If you have Windows developers used to Visual Studio and Unix developers who swear by GNU Make, CMake is (one of) the way(s) to go.
cmake -G"MSYS Makefiles" when compiling for MinGW
cmake -G "Visual Studio 16 2019" -A Win32 when compiling for vs
cmake -G "Visual Studio 16 2019" -A x64 when compiling for vs
The GNU C and C++ compiler are called gcc and g++, respectively.
// Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes)
> gcc hello.c
// (Unixes / Mac OS X) In Bash shell
$ gcc -o hello hello.c
$ chmod a+x hello
$ ./hello
// (Windows) In CMD shell
> g++ -o hello.exe hello.cpp
// Compile and link source hello.cpp into executable hello.exe
> hello
// Execute under CMD shell
// (Unixes / Mac OS X) In Bash shell
$ g++ -o hello hello.cpp
$ chmod a+x hello
$ ./hello
$ g++ -Wall -g -o Hello.exe Hello.cpp
-o: specifies the output executable filename.
-Wall: prints "all" Warning messages.
-g: generates additional symbolic debuggging information for use with gdb debugger.
// Compile-only with -c option
> g++ -c -Wall -g Hello.cpp
// Link object file(s) into an executable
> g++ -g -o Hello.exe Hello.o
-c: Compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object files or libraries.
Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld.exe) to perform the linking.
Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:
g++ -o myprog.exe file1.cpp file2.cpp
However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.