reduce executable size mixing dynamically / static compilations - c++

I am coding a project using c++ under visual c++ 2017 , but I would like to know if I could compile dynamically my project , and add #pragma comment(lib,"ws2_32") only once for all the project my project looks like
./project.cpp
utils.cpp
utils.h
server.cpp
server.h
api.cpp
api.h
60+ files
main.cpp
#include "utils.h"
#include "server.h"
#pragma comment(lib,"ws2_32")
Server server;
int main(int argc, char **argv) {
server.monitoring();
getchar();
return 0;
}
I want to compile all project dinamically , and only add #pragma comment(lib,"ws2_32") and be useful for all the project . I am compiling the project like this cl project.cpp utils.cpp server.cpp api.cpp /DYNAMICBASE:NO /MD /Ot this give me 14k , and it's okay ,but if I change for /MT because I need the program is executed under every windows machine. I got 223k , but I would like to know if I could cut down a little bit more the memory if it's possible ~50k

Related

How to compile a Qt program without qtCreator on Windows?

I have read the question Can I use Qt without qmake or Qt Creator? which is basically the same for Linux, and very useful.
How to compile a basic program using QtCore (console application, even without GUI) on Windows, without using qmake or qtCreator IDE, but just the Microsoft VC++ compiler cl.exe?
For example, let's say we have:
#include <iostream>
#include <QtCore>
int main()
{
QVector<int> a; // Qt object
for (int i=0; i<10; i++)
a.append(i);
std::cout << "hello";
return 0;
}
Using:
call "C:\path\to\vcvarsall.bat" x64
cl main.cpp /I D:\coding\qt\qtbase-everywhere-src-5.15.5\include
fails with:
D:\coding\qt\qtbase-everywhere-src-5.15.5\include\QtCore\QtCore(3): fatal error C1083: Cannot open include file: 'QtCore/QtCoreDepends': No such file or directory
Indeed this file is not present in the release qtbase-everywhere-opensource-src-5.15.5.zip from https://download.qt.io/archive/qt/5.15/5.15.4/submodules/.
TL;DR: More generally, which cl.exe arguments should we use to to able to use all Qt includes, and effectively compile such a minimal project using QtCore?
I finally managed to do it 100% from command line, without the qtCreator IDE, but not yet without qmake. Steps to reproduce:
Let's assume Microsoft MSVC 2019 is installed.
Install qt-opensource-windows-x86-5.14.2.exe. (This is the latest Windows offline installer I could find), double check that you install at least msvc2017_64.
Note: Don't use qtbase-everywhere-opensource-src-5.15.4.zip: using the include subfolder from this package for cl.exe /I ... is not enough. (I thought it would, at first)
Create a folder example containing the main.cpp file above
Open a command line window in this folder and do:
vcvarsall.bat x64
Now either do "c:\path\to\msvc2017_64\bin\qmake.exe" -project to create a example.pro project file or create it manually with:
TEMPLATE = app
TARGET = qt_example
INCLUDEPATH += .
CONFIG += console
SOURCES += main.cpp
Do "c:\path\to\msvc2017_64\bin\qmake.exe". This will create a Makefile file.
Run nmake. This is Microsoft MSVC's equivalent of the make tool.
Copy c:\path\to\msvc2017_64\bin\Qt5Core.dll into the release folder
Run release\example.exe. Working!
Addendum: here is solution now for a minimal GUI app:
main.cpp
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QMessageBox>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox::information(NULL, "Hello", "Hello", "Ok");
return a.exec();
}
qt_example_gui.pro
TEMPLATE = app
TARGET = qt_example_gui
INCLUDEPATH += .
SOURCES += main.cpp
QT += gui widgets
Do the vcvarsall.bat x64, qmake, nmake like in the solution above. No be sure you have this file structure:
release\qt_example_gui.exe
release\Qt5Core.dll
release\Qt5Gui.dll
release\Qt5Widgets.dll
release\platforms\qwindows.dll
Run the .exe, that's it!

Add SFML (third party library) to C++ project on Xcode

I'm learning how to build a simple UI in C++ on my Mac (OS 11.6) using Xcode.
As first step I'm compiling the "Hello world" program, my problem is that the build on Xcode fails but write my own command from terminal, instead, works.
This is the program, I'm using SFML :
#include <iostream>
#include "SFML/Graphics.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I have no error here but when launching Run from Xcode this is the output, in Graphics.hpp file :
#include <SFML/Window.hpp>. //'SFML/Window.hpp' file not found
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/CircleShape.hpp
//other header files
This is how the project is structured ("TestGui" is the project name) :
-TestGui.xcodeproj
-TestGui(folder)
--SFML(directory with all headers file available
-- main.cpp
SFML source code here
So I tried to compile it with my own hands from terminal with :
g++ main.cpp -I ./SFML -o main
and
clang++ main.cpp -I ./SFML -o main
In both cases it compiled, also run worked.
Since the error is linked to a file not found I tried to tell it where libraries are located, so in Xcode from Product->Scheme->Edit Scheme->Run->Arguments->Arguments passed on launch : added -I ./SFML. But the error is still alive.
Added SFML folder to targets from Xcode, didn't copy-pasted but maybe I did it wrong, this is my first time.
EDIT : SFML folder:
--SFML
--- many .hpp files
--- 5 folders (Audio, Graphic, Network, System and Window)
I tried to add also this argument : -L ./SFML but nothing.

Using MATLAB API in C++, matOpen not working

I am writing a piece of code to access MATLAB files, my program compiles but crashes when I call matOpen. When I try to debug the code, the debugger also exits without reaching the offending line of code. I am working in Qt, and I am not too sure if I have done my includes properly.
.pro file
INCLUDEPATH += "C:\Program Files\MATLAB\R2018a\extern\include"
LIBS += "C:\Program Files\MATLAB\R2018a\extern\lib\win64\microsoft\libmx.lib"
LIBS += "C:\Program Files\MATLAB\R2018a\extern\lib\win64\microsoft\libmat.lib"
SOURCES += \
main.cpp
main.cpp
#include <stdlib.h>
#include <vector>
#include <mat.h>
int main(int argc, char *argv[])
{
MATFile *mfPtr;
const char *file = "data.mat";
mfPtr = matOpen(file,"r"); //code runs successfully without this line
return 0;
}
I am compiling using Desktop Qt 5.11.0 MSVC2017 64-bit and my MATLAB version is 2018a. I have also tried deleting the build folder and rebuilding.
Try this
E:\CodePath>gcc *.c -IC:\PROGRA~1\MATLAB\R2018a\extern\include -LC:\PROGRA~1\MATLAB\R2018a\extern\lib\win64\mingw64 -o code -l libmat -l libmx
Place libmat.dll and libmx.dll in the CodePath
I could not get it to work with the Matlab libraries either. For everyone looking for a different approach to write MAT files from C++, I recommend this open source project: https://github.com/jkriege2/TinyMAT

Eclipse can't find source file for debugging

Trying to debug simple c++ project that was checkout like new project from Eclipse IDE and was created like "Makefile Project with existing code" by wizard later.
I placed breakpoints and started debugger. Got error:
No source available for "main() at 0x400c6c"
How to make Eclipse to see source?
UPD
Adding source code:
a.cpp
#include <stdio.h>
int main( int argc, char ** argv )
{
printf( "hello world" );
}
makefile
all:a
a.o:a.cpp
gcc -c a.cpp
Looks like problem in "non Eclipse style" makefile. What is the best way to fix such imported non eclipse project?

Imread causing fatal error on the program

This should be a really straightforward question. When I run the following code:
#include "main.h"
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
namedWindow("k");
imshow("k",image);
waitKey(0);
}
This error shows up when I try to run the imread line. Anyone knows what am I doing wrong here?
Thank's in advace.
Additional Info
This is being done in visual studio 2008 professional editions on a windows 7 32-bit machine.
I'm trying to debug the "Release" version.
This is the main.h file:
#pragma once
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#pragma warning( disable: 4996 )
#include <cv.h>
#include <highgui.hpp>
#include <core.hpp>
My additional include directories (Configuration Properties -> C\C++ -> General) are:
...\OpenCV2.2\include\opencv2\highgui
...\OpenCV2.2\include\opencv2\
...\OpenCV2.2\include\opencv
...\OpenCV2.2\include
The (...) are merely for my privacy, the actual code contains the full path.
My "Additional Dependencies" (Configuration Properties -> Linker -> Input) are:
"...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_core220.lib"
"...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_highgui220.lib"