Notes for Beginners of Software Development on Linux

Linux is a great platform for software development targeting servers or backends. In general, working on Linux is very productive. The problem that beginners on Linux face is the the learning curve is steep at the beginning. But believe me, after you get through the initial green steep learning step as in the figure below with some hard work, you will feel like working on a rocket in the yellow part. » Read more

RAII-like Error Handling and Resource Management in C

Error handling and resource management are pervasive in programs. RAII originated in C++ is great. With RAII, it is much easier to write easy-to-read code that allocats/deallocats resources in the constructor/destructors. By representing a resource with a local object, we are sure that local object’s destructor will release the resource and will not forget to release the resource even there are exceptions. » Read more

How to Statically Link C and C++ Programs on Linux with gcc

Before statically linking you C and C++ programs, you should be aware of the drawbacks of the static linking especially with glibc. There are some good discussions already: with glibc you’re linking static programs which are not really static and some others here and here. That said, you can choose to statically link C and C++ programs on Linux, only when you know what you are doing and why. » Read more

Java与C++在语言方面上的不同

1,Java抛弃了头文件、预处理机制、goto语句和多维数组。 2,Java不支持指针。 3,Java抛弃了联合体和结构体。 4,Java不支持独立函数。所有函数必须在类中声明。 5,Java不支持多重继承,可以使用接口模拟多重继承。 6,Java不支持运算符重载。 7,Java中布尔型不再用整数来代替。 8,Java中主函数必须有一个字符串类型的参数。 Java抛弃的C++中的这些机制和结构多数都是“危险”的,减少了语言的复杂性,增强了安全性,虽然在一定程度上减少了其灵活性。 » Read more

Hashing Library for C

I try to find some Hashing libraries for C and find several good ones. The hsearch function in the GNU C library. There are other methods to organize information which later should be searched. The costs of insert, delete and search differ. One possible implementation is using hashing tables. The following functions are declared in the header file search.h. CMPH – C Minimal Perfect Hashing Library. » Read more

Inline Assembly with GCC on Linux

One cool feature of gcc is that it can inline assembly into C code. With inline assembly, the programmer can precisely control the execution of the processor, such as forcing variables to use registers, getting special processor state efficiently, and writing critical efficient code in assembly by hand. I compile a list of tutorials from the Internet about inline assembly with gcc on Linux. » Read more

GNU C Reference Manual

When we program in C, a C reference by hand is usually useful. The GNU C Reference Manual provides us a good reference for the C programming language implemented by the GNU C Compiler. “This manual is strictly a reference, not a tutorial. Its aim is to cover every linguistic construct in GNU C.” GNU also provides a reference manual for C library in separated documents. » Read more

Vim Indenting C/C++ Code

Vim provides some basic formatting commands. A combination of these commands with the editing commands will make the programmer happy. A list of basic text formatting commands in Vim:= is an operator that formats/indents text. i{ is a text object that specifies the surrounding code block. It should be used together with v, =, etc.Some basic but useful operation that are combination of the two basic ones above:vi{ visually selects the inner code block around the cursor. » Read more

fclose – Close a Stream

fclose is a frequently used C standard library which closes the file associated with the stream and disassociates it. NAME fclose – close a stream SYNOPSIS #include <stdio.h> int fclose(FILE *fp); DESCRIPTION The fclose() function will flushes the stream pointed to by fp (writing any buffered output data using fflush()) and closes the underlying file descriptor. RETURN VALUE Upon successful completion 0 is returned.  » Read more

GNU glibc Manual

“The C language provides no built-in facilities for performing such common operations as input/output, memory management, string manipulation, and the like. Instead, these facilities are defined in a standard library, which you compile and link with your programs. The GNU C library, described in this document, defines all of the library functions that are specified by the ISO C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to the GNU system.” “The GNU C library includes several header files, each of which provides definitions and declarations for a group of related facilities; this information is used by the C compiler when processing your program. » Read more

Win32下COM口通信控制

这段时间做兼职帮做了一个COM口单片机监控控制程序, 学习了些Win32下COM口的控制. 下面是与COM口控制有关的部分示例代码. //打开COM1 hCOM=CreateFile( "COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (hCOM==INVALID_HANDLE_VALUE) { MessageBox( GetForegroundWindow(),"无法打开串口!","操作失败",MB_ICONINFORMATION); return; }//设置DCB DCB dcb; if (!GetCommState(hCOM,&dcb)) { MessageBox( GetForegroundWindow(),"无法获取串口状态!","操作失败",MB_ICONINFORMATION); hCOM=INVALID_HANDLE_VALUE; return; }dcb.BaudRate=9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; if (!SetCommState(hCOM,&dcb)) { MessageBox( GetForegroundWindow(),"无法设置串口状态!","操作失败",MB_ICONINFORMATION); hCOM=INVALID_HANDLE_VALUE; return; }//设置COMMTIMEOUTS COMMTIMEOUTS communication_timeout; communication_timeout.ReadIntervalTimeout=MAXDWORD; communication_timeout.ReadTotalTimeoutMultiplier=0; communication_timeout.ReadTotalTimeoutConstant=0; communication_timeout.WriteTotalTimeoutMultiplier=0; communication_timeout.WriteTotalTimeoutConstant=0; if (!SetCommTimeouts(hCOM,&communication_timeout)) { MessageBox( GetForegroundWindow(),"无法设置串口超时!","操作失败",MB_ICONINFORMATION); hCOM=INVALID_HANDLE_VALUE; return; }//向串口写入ABC三个字符 ULONG nBytesWritten; WriteFile(hCOM,"ABC",3,&nBytesWritten,NULL);//从串口读入三个字符至str char str[3]; ULONG bytes_read_num; ReadFile(hCOM,str,3,&bytes_read_num,NULL); » Read more

Win32 Programming: Operation on Files

Here, we show some example code on Win32. The code are operations on file on Win32 OSes. The code here uses some MFC classes. Recursively show the files from a path: void Show(const char *folderPath) { CFileFind finder; CString path(folderPath); path += "*.*"; BOOL bWorking = finder.FindFile(path); while (bWorking) { bWorking = finder.FindNextFile(); cout << finder.GetFilePath() << endl; } finder.Close(); }Recursively copy a folder:bool CopyFolder(const char *srcFolderPath, const char *dstFolderPath) { CFileFind srcFinder, dstFinder; CString src(srcFolderPath); CString dst(dstFolderPath); if (!CreateDirectory(dst.GetBuffer(), 0)) return false; CFileFind finder; BOOL bWorking = finder.FindFile(src + "*.*"); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; CString fileName = finder.GetFileName(); if (finder.IsDirectory()) { if (!CopyFolder((src + "" + fileName).GetBuffer(), dst + "" + fileName)) { finder.Close(); return false; } } else { if (!CopyFile(src + "" + fileName, dst + "" + fileName, 0)) { finder.Close(); return false; } } } finder.Close(); return true; }Recursively delete a folder and the content in it:bool DeleteFolder(const char *folderName) { CString path(folderName); CFileFind finder; BOOL bWorking = finder.FindFile(path + "*.*"); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; CString fileName = finder.GetFileName(); if (finder.IsDirectory()) { if (!DeleteFolder((path + "" + fileName).GetBuffer())) { finder.Close(); return false; } } else { if (!DeleteFile((path + "" + fileName).GetBuffer())) { finder.Close(); return false; } } } finder.Close(); return RemoveDirectory(folderName); } » Read more

SetProxy: 一个设置IE代理的命令行小工具

IE的代理设置用起来并不方便,我自己而言就要经常更改代理服务器,相信很多人有跟我相同的需要。使用C++编写了一个小程序SetProxy调用Win32 API来设置代理服务器IP,用起来方便多了。 编译后为一个可运行console程序,将它放在\windows\system32目录下,使用时只要运行 SetProxy IP:port 就可以把IP设置好,如果要关闭代理,只需运行 SetProxy "" 命令行中运行,界面较土,但用着方便。可以考虑设置几个快捷方式在桌面或者工具栏,点击即设置代理,其它方式发挥想象。 程序下载地址. 源代码也放在这里,希望有需要的人可以用得上 ;) 源代码下载地址. 这是一份有些年头的代码, 如果你在较新的编译器下编译这个项目, 请先做这里所述的小修改. — Eric on Apr. 9, 2014. » Read more

DebugTerm – telnet二进制显示与发送调试程序

由于做COM控制程序,调试程序时需要发送和接收二进制形式数据。手动编制了一个可以发送和接收二进制数据的DebugTelnet程序。显示为十六进制,显示和发送均以字节为单位。 例如: Connect to bbs.fudan.edu.cn : 23 ... Connected. Received: 0AC4BFC7B0D5FDD3D030C8CBD5FDB3A2CAD4C1ACBDD3FFFB01FFFB03 Received: ... 84 Received: 84 下载地址 此程序免费使用. » Read more

Convert string to int and Reverse in C and C++

This post introduces how to convert string to int and int to string in C and C++. Convert string to int 1. The C way Use C standard library #include <cstdlib> #include <string> std::string text = "123"; int number = std::atoi (text.c_str ()); if (errno == ERANGE) //or may be std::errno { // the number is too big/small // number = LONG_MAX or LONG_MIN } else if (errno == ????) //maybe EINVAL, E2BIG or EDOM (or ERANGE again) { //unable to convert to a number } 2. » Read more

MFC程序使用系统风格界面

VC6默认编译出来的程序在XP下Luma风格下运行也是Windows的经典界面, 有损界面的美观与统一. VC2008默认设置下如果不是使用的unicode也是如此. 本文给出使VC6和VC2008可以编译出使用系统界面风格的解决方案. 1. 使VC6编译出使用系统风格的程序 步骤如下: 1) 创建一个.manifest文件的资源. 在res/文件夹下创建一个跟以程序名加.manifest的文件, 如果程序为test.exe, 则创建test.exe.manifest 文件可由此下载: https://www.systutorials.com/t/g/programming/resultcollector.manifest/ 注意要使用utf-8编码保存。 2) 将新定义的资源加入到.rc2文件中, 类型设为24. 打开res/文件夹下的.rc2文件, 在其中加入如下定义: 1 24 MOVEABLE PURE "res/test.exe.manifest" 其中的文件地址按1)步中修改的设置即可. 之后编译即可, 为了使程序界面可能充分利用系统的界面特性, 可以将界面字体设置为TrueType类型的, 利用Windows XP等系统的屏幕字体平滑特性. 2. 使VC2008编译出使用系统风格的程序 在VC2008下就比较简单了, 如果程序字符集使用unicode则默认就是使用系统界面风格的, 如果选择其它的类型, 则编辑下stdafx.h即可. 最后面部分找到这么一段: #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"") #elif defined _M_IA64 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'"") #elif defined _M_X64 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'"") #else #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"") #endif #endif 修改为如下即可: #if defined _M_IX86 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"") #elif defined _M_IA64 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'"") #elif defined _M_X64 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'"") #else #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"") #endif Updated history 7 Mar. » Read more