Use Source Insight as the editor to develop Unix softwares

Source Insight is my favorite editor, and I have used it for more than 10 years. But when employing it to develop Unix software, you will run into annoying line break issue, which is on windows, the newline is \r\n while in Unix it is \n only. Therefore you will see the file edited in Source Insight will display an extra ^M in Unix environment:

#include <stdio.h>^M
^M
int main(void)^M
{^M
        printf("\r\n");^M
}^M

To resolve this problem, you can refer this topic in stackoverflow:

To save a file with a specific end-of-line type in Source Insight, select File -> Save As…, then where it says “Save as type”, select the desired end-of-line type.

To set the end-of-line type for new files you create in Source Insight, select Options -> Preferences and click the Files tab. Where it says “Default file format” select the desired end-of-line type.

So you can set Unix file format as you wanted:

Capture

Another caveat you should pay attention is if you use git Windows client, by default, it will convert the newline of project from \n to \r\n directly. My solution is just disabling this auto conversion feature:

git config --global core.autocrlf false

How to count the line number of a file?

How to count the line number of a file? It seems an easy question for Unix guys: using wc command. Let’s see an example:

# cat test
aaaa
bbbb
cccc
# wc -l test
3 test

It displays right. But wait a minute, let’s check the wc -l option meaning:

# wc --help
......
-l, --lines            print the newline counts
......

wc -l just print the newline counts, so it increases newline count as the line number. Using hexdump to see the content of test:

# hexdump -C test
00000000  61 61 61 61 0a 62 62 62  62 0a 63 63 63 63 0a     |aaaa.bbbb.cccc.|
0000000f

No problem, test contains 3 newlines. But now, the question is here, how about the last line doesn’t contain newline?

Let me do the following experiment:

# echo -n "a" > test
# hexdump -C test
00000000  61                                                |a|
00000001
# wc -l test
0 test

Now the test file only contains 1 character, and no newline. This time, wc -l thinks there is no line in test file.

Based on the previous discussion, maybe awk is a better tool when using to count line number of file. Just one statement:

# awk 'END {print NR}' test
1

It outputs right line number.

Build Luajit notice

When uploading Luajit (for example, v2.0.4) from Windows to Linux, you may get the following compile error:

In file included from lj_ffrecord.c:859:0:
lj_recdef.h:224:1: error: ‘recff_rawlen’ undeclared here (not in a function)
 recff_rawlen,
 ^
Makefile:645: recipe for target 'lj_ffrecord.o' failed

The root cause is in src/host/buildvm_lib.c:

void emit_lib(BuildCtx *ctx)
{

    ......
    int ok = 1;
    if (!strcmp(buf, "#if LJ_52\n"))
      ok = LJ_52;
    else if (!strcmp(buf, "#if LJ_HASJIT\n"))
      ok = LJ_HASJIT;
    else if (!strcmp(buf, "#if LJ_HASFFI\n"))
      ok = LJ_HASFFI;
    ......

}

Because in Windows, the EOL(End-of-Line) should be “\r\n“, so the !strcmp(buf, "#if LJ_52\n") will return false (!strcmp(buf, "#if LJ_52\r\n") will return true). The modify method is using the dos2unix tool to convert the whole folder:

# cd LuaJIT-2.0.4
# find . -type f -print0 | xargs -0 dos2unix

Then compile will be OK.

Reference:
(1) The EOL difference in Windows and UNIX may generate compile error;
(2) How can I run dos2unix on an entire directory?.