Wednesday, March 27, 2019

C++ Modules

Modules will be included in C++ 20!


I just tried a small sample on clang-9 and it worked out of the box:


1. Create file test.cppm:


export module M;

export int sum(int op1, int op2)
{
        return op1 + op2;
}


2. Compile the module with:

./clang++ -fmodules-ts --precompile test.cppm --std=c++2a -o M.pcm

./clang++ -fmodules-ts -c M.pcm -o M.o


3. Create file main.cpp:

import M;

#include <iostream>
using namespace std;

int main()
{
        cout << sum(2, 3);

        return 0;
}



4. Compile with:

./clang++ -fmodules-ts -fprebuilt-module-path=. M.o main.cpp -o sample --std=c++2a

5. Run

./sample
5


What a great time to be a C++ programmer. :-)

Tuesday, June 20, 2017

How to query ElasticSearch using curl

curl -H "Content-Type: application/json" -X POST http://localhost:9200/test2/type1 -d '{ "name": "Marina" }' 


curl -H "Content-Type: application/json" -X GET http://localhost:9200/test2/_search 

Friday, April 28, 2017

Executable sizes

I was curious about the size of executables generated by different compilers in C and C++. So I made a small program that reads a list of numbers from stdin and prints the sum, then compiled it using several compilers and settings. You can find below the results.


Language Operating system Compiler Runtime dependencies Executable size in bytes
C++ Windows 10 Visual C++ 2017 Dynamic 10,752
C++ Windows 10 Visual C++ 2017 Static 290,816
C Windows 10 Visual C++ 2017 Dynamic 10,752
C Windows 10 Visual C++ 2017 Static 140,288
C++ Ubuntu 16.10 GCC 6.2 Dynamic 8,960
C++ Ubuntu 16.10 GCC 6.2 Static 2,202,384
C++ Ubuntu 16.10 GCC 6.2 Static+strip 1,771,096
C Ubuntu 16.10 GCC 6.2 Dynamic 8,528
C Ubuntu 16.10 GCC 6.2 Static 915,608
C Ubuntu 16.10 GCC 6.2 Static+strip 844,784
C++ Ubuntu 16.10 clang 4.0.0 Dynamic 8,984
C++ Ubuntu 16.10 clang 4.0.0 Static 2,202,320
C++ Ubuntu 16.10 clang 4.0.0 Static+strip 1,771,056
C Ubuntu 16.10 clang 4.0.0 Dynamic 8,352
C Ubuntu 16.10 clang 4.0.0 Static 915,528
C Ubuntu 16.10 clang 4.0.0 Static+strip 844,744


The command line used:

clang++ -o main Main.cpp -O3 -static -static-libgcc -static-libstdc++

clang -o main Main.c -O3 -static -static-libgcc

How to static link C and C++ runtimes in gcc 6.2

For C:

gcc -o main Main.c -O2 -static-libgcc -static


For C++:

g++ -o main Main.cpp -O2 -static-libgcc -static-libstdc++ -static


To make sure it worked:


ldd main


should not display any dependencies.

Saturday, May 7, 2016

How to dump audio using mplayer


mplayer -dvd-device c:\dvd_pupileira\VIDEO_TS dvd://1 -dumpaudio -dumpfile audio.ac3

Saturday, January 31, 2015

How to find development libraries for Ubuntu

Today I was compiling boost in my Ubuntu Linux 14.10 and got the following error:
libs/iostreams/src/bzip2.cpp:20:56: fatal error: bzlib.h: No such file or directory

Then I searched the web and found a nice explanation in this site.

I'll describe below a generic procedure to find which Ubuntu package contains some header file:

1) Install apt-file:
sudo su
apt-get install apt-file
apt-file update
2) Search for packages containing the header file:
apt-file search bzlib.h
3) Install the package:
apt-get install libbz2-dev

Welcome

Hi!

I'm starting this blog to keep track of things that may be useful for me and possibly for others.

My main interests are software development, C++, Formula 1 and racing games / simulators.

I have created another blog for posts in Brazilian Portuguese.

You can contact me through the e-mail cmello @ gmail dot com.
Thank you for the visit!