Use gcc for C code
Use g++ for C++ code.
If you have Linux or use Cygwin you will have the GNU Compiler Collection, which provides both gcc and g++. Traditionally, C programs end in dot c and C++ programs end in dot cc or dot cpp.
Some simple C++ code might compile with gcc, but eventually you will get linker errors like this:
undefined reference to ‘__gxx-personality_v0’
collect2: error: ld returned 1 exit status
If you use g++ and still get this error, try
g++ file.cc -lstdc++
because your compiler is having trouble seeing some C++ Standard Library functions
per
https://stackoverflow.com/questions/6045809/link-error-undefined-reference-to-gxx-personality-v0-and-g
I was glancing at a book on C that mentioned gcc, but did not mention g++, so I thought I should mention this to you if you are starting out as a beginning programmer.
Robert
Thursday, August 24, 2017
Thursday, August 17, 2017
Making a Unique File name
I overheard one fellow asking another fellow for help in creating unique file names. He wanted a base name with a random number attached at the end. This happens when you are doing logging and you do not want to over-write the old file or have to delete the old file before you create a new one. Well, you do not need a random number generator. You can just add a number to the file name that is different each time you run the program that generates the log file. Imagine running a test program several times during a day and you want to harvest all the log files.
Below is a small program that generates files with names like this:
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt
Epoch time in C is the total number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC (Greenwich mean time). With 5 digits the numbers will not "roll over" until after 100,000 seconds, meaning 27.7 hours. The randomness comes from running the logging program at different times. Usually after 8 hours of testing you harvest the log files for study and clear out the old files.
The simple program in C that has this output follow. You can copy and paste this code and run it to test it. You can incorporate the ideas from this code into your own code when you write a file that will be written repeatedly during the day. This gives you multiple copies of the file, which is important when the output to the file is changing from iteration to iteration. I hope you find this helpful. I license this code to you free of charge via the MIT license, which appears at the very end.
Robert
-----------------------------------------------
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // sleep()
// compile with gcc file_unique_id.c -o file_unique_id
int main ()
{
char* base_name = "test_file";
char* extention = ".txt";
// these string lengths are arbitrary
char long_string[200]; // holds file name
char short_string[100];// hold time in a string
FILE* fd;
int chopped_time;
time_t test_time;
int index;
for (index = 0 ; index < 5; ++index)
{
test_time = time(0); // get epoch time
chopped_time = (test_time % 100000); // keep the smallest 5 digits
// add those 5 digits to the base file name
sprintf(long_string, "test_file_%d", chopped_time);
// add the file extension
// long_string is now the file name with a unique 5 digit number
strcat(long_string, extention);
// short_string holds the full epoch time value
sprintf(short_string, "%d\n", test_time);
// log short_string to file whose name is in long_string
fd = fopen(long_string, "w");
fwrite(short_string, strlen(short_string), 1, fd);
fclose(fd);
sleep(3);
}
return 0;
}
/**
* Sample outputs of this program
./file_unique_id
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt
cat test_file_27023.txt
1503027023
**/
Copyright 2017 Robert Canright
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Below is a small program that generates files with names like this:
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt
Epoch time in C is the total number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC (Greenwich mean time). With 5 digits the numbers will not "roll over" until after 100,000 seconds, meaning 27.7 hours. The randomness comes from running the logging program at different times. Usually after 8 hours of testing you harvest the log files for study and clear out the old files.
The simple program in C that has this output follow. You can copy and paste this code and run it to test it. You can incorporate the ideas from this code into your own code when you write a file that will be written repeatedly during the day. This gives you multiple copies of the file, which is important when the output to the file is changing from iteration to iteration. I hope you find this helpful. I license this code to you free of charge via the MIT license, which appears at the very end.
Robert
-----------------------------------------------
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // sleep()
// compile with gcc file_unique_id.c -o file_unique_id
int main ()
{
char* base_name = "test_file";
char* extention = ".txt";
// these string lengths are arbitrary
char long_string[200]; // holds file name
char short_string[100];// hold time in a string
FILE* fd;
int chopped_time;
time_t test_time;
int index;
for (index = 0 ; index < 5; ++index)
{
test_time = time(0); // get epoch time
chopped_time = (test_time % 100000); // keep the smallest 5 digits
// add those 5 digits to the base file name
sprintf(long_string, "test_file_%d", chopped_time);
// add the file extension
// long_string is now the file name with a unique 5 digit number
strcat(long_string, extention);
// short_string holds the full epoch time value
sprintf(short_string, "%d\n", test_time);
// log short_string to file whose name is in long_string
fd = fopen(long_string, "w");
fwrite(short_string, strlen(short_string), 1, fd);
fclose(fd);
sleep(3);
}
return 0;
}
/**
* Sample outputs of this program
./file_unique_id
test_file_27023.txt
test_file_27026.txt
test_file_27029.txt
test_file_27032.txt
test_file_27035.txt
cat test_file_27023.txt
1503027023
**/
Copyright 2017 Robert Canright
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Monday, May 29, 2017
The Importance of Commenting Code
I recently read this article on commenting code: Is There a Correct Way to Comment Your Code? by Erik Dietrich. The opinion that “clean code needs no comments” was mentioned. No. I disagree. Comments are helpful. If a software product is successful then it will be in use for many years. Programmers unfamiliar with the old code will need to quickly find the spots in the code that need to be changed to fix problems or to add new features. This activity is called "software maintenance." Complex technical code is full of equations. Complex equations are meaningless to programmers and comments are needed to explain the purpose and workings of the equations. (Complex equations are usually provided to programmers by systems engineers or R&D scientists.)
Complex code can benefit from comments that provide a concise design description. These design comments might be concentrated at the start of the software module, or they might be scattered among the methods within the software module.
The primary tool for software maintenance is the grep tool. The least of comments is a keyword in the code that can be caught with grep. For example, imagine you are ordered to modify Capability_101 in order to add a new Capability_102 to the old code. As a code maintainer you need the code that implements the old capability tagged with a comment that says "Capability_101" so the grep tool can find it.
To say well written code needs no comments is foolishness.
Robert
I have written about comments in code before:
Self Documenting Code and Ecclesiastes October 13, 2013
Software Maintenance and Variables March 16, 2014
Sunday, May 28, 2017
Interested in flight, aircraft, or physics?
JSBSim is an open source flight dynamics model (FDM) that compiles and
runs under many operating systems, including Microsoft Windows, Apple
Macintosh, Linux, IRIX, Cygwin (Unix on Windows), etc. The FDM is
essentially the physics/math
model that defines the movement of an aircraft, rocket, etc., under the
forces and moments applied to it using the various control mechanisms
and from the forces of nature. JSBSim has no native graphics. It can be
run by itself as a standalone program, taking
input from a script file and various vehicle configuration files. It
can also be incorporated into a larger flight simulator implementation
that includes a visual system.
http://jsbsim.sourceforge.net/
Robert
http://jsbsim.sourceforge.net/
Robert
Free AI Code for Chess
Here is a link to an article from CodeProject, "Test Driven Chess Artificial Intelligence" by Kristian Ekman: https://www.codeproject.com/Articles/1168892/Test-Driven-Chess-Artificial-Intelligence
From the article: This is a fully functional yet simple chess program that aims to help you understand how a chess engine works. There are already open source chess engines on the Internet which focus on high performance. This is a standard object oriented C# solution that is meant to be easier to comprehend. The focus has not been to make a fast and high rated chess engine. I have developed a working chess AI that plays descent good moves with code that you hopefully like to read. A few of the more specific goals have been to correctly implement Alpha Beta Pruning and Zobrist Hashing using C# 6.
If you are interested in artificial intelligence or computer chess, you might enjoy this.
Robert
From the article: This is a fully functional yet simple chess program that aims to help you understand how a chess engine works. There are already open source chess engines on the Internet which focus on high performance. This is a standard object oriented C# solution that is meant to be easier to comprehend. The focus has not been to make a fast and high rated chess engine. I have developed a working chess AI that plays descent good moves with code that you hopefully like to read. A few of the more specific goals have been to correctly implement Alpha Beta Pruning and Zobrist Hashing using C# 6.
If you are interested in artificial intelligence or computer chess, you might enjoy this.
Robert
Saturday, May 27, 2017
Why Learn Pascal?
I purchased a copy of Genetic Algorithms in Search, Optimization & Machine Learning by David E. Goldman (1989). The book has sample code in Pascal. This is where Free Pascal comes in. When I coded last in Pascal it was with Turbo Pascal and I enjoyed it much more than coding in C. (I mentioned Free Pascal back in December 22, 2012 in this post: Learning to Program with Pascal.)
If you go to the Free Pascal website and look at their documentation you can see there is a great deal of documentation and that Free Pascal is now an object oriented language. You can download a copy of the language (free under the GNU General Public License). My complaint against the Genetic Algorithms book by Goldman is not that pascal is used, but that there is no license. I have looked at the book and there is no license that permits you to use the code. So if I typed up the code and posted it online at Sourceforge or Git Hub I would be violating copyright law, so I cannot share the code. And the code is not available online. You have to type the code yourself. There was an operating systems textbook for an OS called Xinu that had a statement from the author that the code could be used and I saw people use this OS professionally, typing the code from the book.
Criticisms against Pascal being old are misguided. The Scheme programming language is old, but it is a great educational language. The book Structure and Interpretation of Computer Programs by Harold Abelson et al is a great book to study. The Scheme programming language is also free.
So do not hesitate to get the book Genetic Algorithms by Goldman because the code is in Pascal. Pascal is a nice language worth knowing and Free Pascal can be used to run the code in the book.
Robert Canright
If you go to the Free Pascal website and look at their documentation you can see there is a great deal of documentation and that Free Pascal is now an object oriented language. You can download a copy of the language (free under the GNU General Public License). My complaint against the Genetic Algorithms book by Goldman is not that pascal is used, but that there is no license. I have looked at the book and there is no license that permits you to use the code. So if I typed up the code and posted it online at Sourceforge or Git Hub I would be violating copyright law, so I cannot share the code. And the code is not available online. You have to type the code yourself. There was an operating systems textbook for an OS called Xinu that had a statement from the author that the code could be used and I saw people use this OS professionally, typing the code from the book.
Criticisms against Pascal being old are misguided. The Scheme programming language is old, but it is a great educational language. The book Structure and Interpretation of Computer Programs by Harold Abelson et al is a great book to study. The Scheme programming language is also free.
So do not hesitate to get the book Genetic Algorithms by Goldman because the code is in Pascal. Pascal is a nice language worth knowing and Free Pascal can be used to run the code in the book.
Robert Canright
Wednesday, May 24, 2017
Comparing Files in Notepad++
A couple of years ago I posted an article about installing a Compare plugin for Notepad++. The links I used in the past have died, so here is new information.
On my copy of Notepad++, I did Plugins > Compare > About and got the window I show above. So you can go to Source Forge (https://sourceforge.net/) and search on "compare notepad plugin" and you will see you can download Compare Plugin 1.5.6.2.bin.zip (https://sourceforge.net/projects/npp-compare/?source=directory). Also there is a Notepad++ web page (https://notepad-plus-plus.org/community/topic/11116/compare-plugin) that lets you download Compare Plugin 1.5.6.7.bin.zip
This website (http://docs.notepad-plus-plus.org/index.php?title=Plugin_Development#How_to_install_a_plugin) has these instructions on how to install a Notepad++ plugin:
These are generic instructions. If the plugin comes with some different procedure, that procedure should be followed instead.
Once you installed the plugin, you can use (and you may configure) it via the menu "Plugins".
KDiff3
The KDiff3 tool is a little better. It compares directories as well. I am working a job now where the client provided me with a computer that had Notepad++ installed, but not KDiff3 and the client would not give me admin privileges to install KDiff3. So I am using the Compare plugin for Notepad++ and I'm glad I have it.
Here's what it looks like (below). Single-click on the image at it will enlarge.
On my copy of Notepad++, I did Plugins > Compare > About and got the window I show above. So you can go to Source Forge (https://sourceforge.net/) and search on "compare notepad plugin" and you will see you can download Compare Plugin 1.5.6.2.bin.zip (https://sourceforge.net/projects/npp-compare/?source=directory). Also there is a Notepad++ web page (https://notepad-plus-plus.org/community/topic/11116/compare-plugin) that lets you download Compare Plugin 1.5.6.7.bin.zip
This website (http://docs.notepad-plus-plus.org/index.php?title=Plugin_Development#How_to_install_a_plugin) has these instructions on how to install a Notepad++ plugin:
How to install a plugin
The plugin (in the DLL form) should be placed in the \plugins subfolder of the Notepad++ Install Folder. Configuration files should go in \Plugins\Config. Documentation files should go to \Plugins\Doc\ directory.These are generic instructions. If the plugin comes with some different procedure, that procedure should be followed instead.
Once you installed the plugin, you can use (and you may configure) it via the menu "Plugins".
KDiff3
The KDiff3 tool is a little better. It compares directories as well. I am working a job now where the client provided me with a computer that had Notepad++ installed, but not KDiff3 and the client would not give me admin privileges to install KDiff3. So I am using the Compare plugin for Notepad++ and I'm glad I have it.
Here's what it looks like (below). Single-click on the image at it will enlarge.
Subscribe to:
Posts (Atom)

