How to make Mac OS X and MySQL happy with Qt

Using shared libraries on Mac OS X is one of the things that is actually a lot harder than on Windows.  For example, you cannot just put the shared library for MySQL into your path or into the working directory. You have to change the plugin file itself to make it look in the correct location. You find the current value using the command “otool” and change the value using the command “install_name_tool”. Here is an example:

cd Qt/5.4/clang_64/plugins/sqldrivers/

otool -L libqsqlmysql.dylib 

libqsqlmysql.dylib:
libqsqlmysql.dylib (compatibility version 0.0.0, current version 0.0.0)
/opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
...

install_name_tool -change /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.20.dylib ./libqsqlmysql.dylib 

otool -L libqsqlmysql.dylib 

libqsqlmysql.dylib:
libqsqlmysql.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/local/mysql/lib/libmysqlclient.20.dylib (compatibility version 18.0.0, current version 18.0.0)
...

Organized C code – Part 1 – The Headers

This is a step-by-step guide to writing code in C and keeping it organized as you go.  It will help you write code that can be reused on multiple projects and hardware platforms.  I will explain some of the finer details along the way so, technically, you don’t even need to know how to program in C to use this guide.

This first part will just go through some templates that every set of C header files will start with.  These header files are used to pre-define the C code you will be writing in a future part of this guide.  That is it for the introductions, now on to your first step writing organized C code!

Step 1

Create a directory for your files with two subdirectories: include and src.  For example, my directory structure looks like this:

directory structure

organized/
organized/include/
organized/src/

The include directory will store all of your header (.h) files and the src directory will store all of your source (.c) files.  These are the files that the C compiler will use to generate machine code (binary language) for your computer.  The C compiler has a “preprocessor” that will look through the code and make changes before attempting to generate machine code.  The C preprocessor is your friend and I will refer to it often.

Step 2

If you need a program to write files, I suggest Microsoft’s Visual Studio Code available here.

Create a text file for the public header that ends with .h to indicate it is a header file.  Save this file in your include directory. For example, the path to my file is:

organized/include/organized.h

snap_2016-10-04_13h51m22s_003_organized-h-visual-studio-code

Paste the following code into your file.  This is the code needed for every header file.  Each time you use this code, replace “ORGANIZED_H” with something that matches your file name.  The only requirement is that you make it unique.  The same name cannot be defined by any other code within a project that uses this header file and I will explain why below.

#ifndef ORGANIZED_H
#define ORGANIZED_H
#ifdef __cplusplus
extern "C" {
#endif
// public header code goes here
#ifdef __cplusplus
}
#endif
#endif // ORGANIZED_H
// empty line

Here are details on this template line by line:

  1. #ifndef is a command used by the C preprocessor that means “If not defined”.  It is used here to check for the name that uniquely identifies this header file so that the entire file can be skipped if it has already been included in the project.  There are other ways to do this, but this method is the only one that is supported by ALL C compilers.
  2. #define is a command used by the C preprocessor that means “Define this value”.  It is used here to define the name you have chosen, which means the next time this header file is included somewhere in the project, the name will already be defined.
  3. #ifdef is a command used by the C preprocessor that means “If defined”.  It is used here to check if the compiler is actually a C++ compiler.  A C++ compiler can do everything a C compiler can do (and more), but it likes to keep things separate.  That is why we will add some code only if this is a C++ compiler.
  4. extern is a keyword that tells the compiler the following code should be made available throughout the program and not just to this file.  It is used here because a C++ compiler was detected at line 3 and we want all of our header code to be made available to C and C++ alike.  The { open brace starts a group of code (code block) that will be made accessible because of the extern keyword.
  5. #endif is a command used by the C preprocessor that means “End if”.  It is used here to end the #ifdef statement from line 3.  Every #if statement requires a matching #endif
  6. // is a single line comment.  Almost all C compilers support this.  If you want to be extra careful that your code will be portable (supported by ALL C compilers), you should write all comments like this instead:
    /* public header code goes here */
  7. #ifdef is here again to ensure we only write the code below it if this is a C++ compiler
  8. } is a closed brace that ends the extern group started at line 4
  9. #endif ends the #ifdef at line 7
  10. #endif ends the #ifndef at line 1.  The single line comment // to the side of the #endif makes it easy to find the #if statement that is being ended here.  This is because there could be hundreds of lines of code between the #if and #endif so it is not easy to read without the extra comment.
  11. This line should be empty.  This webpage won’t let me show it, but this should be an empty line.  If the last line in your file is not empty, the preprocessor could accidentally put code right next to it without any indication that it should have been separate.  If any of your .h or .c files do not have an empty line at the end, go add them now.

Step 3

Create a text file for the private header that ends with .h to indicate it is a header file.  Save this file in your include directory.  For example, the path to my file is:

organized/include/organized_private.h

Paste the header template code into your file and change the #define name and the comment to indicate this is your private header.  For example, I have changed lines 1,2,6,and 10:

#ifndef ORGANIZED_PRIVATE_H
#define ORGANIZED_PRIVATE_H
#ifdef __cplusplus
extern "C" {
#endif
// private header code goes here
#ifdef __cplusplus
}
#endif
#endif // ORGANIZED_PRIVATE_H
// empty line

That is it for Part 1.  You can download the sample code here.

In the next part we will start to fill-in the public header file.

Google Gmail access via POP3 gives ERR [SYS/TEMP]

Have you ever received this error from pop.gmail.com?

-ERR [SYS/TEMP] Temporary system problem. Please try again later.

I did.  It turns out that if you try to use SSL on port 995 and login using the USER and PASS commands, you will receive this error if you either: 1. tried to base64 encode your user/password data or 2. you did not setup an “App password” and use it instead of your normal password.

You can setup an “App password” here

That’s it for now.  Good luck!

 

Mac OS X Board thingdev (platform esp8266, package esp8266) is unknown

Here is a quick fix if you run into the error:
“Board thingdev (platform esp8266, package esp8266) is unknown”

This will remove all of your Arduino boards and preferences

1. open a terminal
2. type cd to access your home directory
3. type cd Library
4. type rm -rf Arduino15/ to remove all Arduino settings
5. follow the instructions found here to reinstall:
https://github.com/esp8266/Arduino

Using LUA in a C++, Visual Studio 2013, Qt, or any C/C++ application

Start Super-Quick-Start Guide:

  1. Download and extract the LUA source.  Click here
  2. Add all of the .c files in the “src” directory to your project except for lua.c and luac.c (these are the interpreter and compiler that are not needed for this example and, depending on your use, are not needed at all)
  3. Add the path to the LUA headers to your project include paths. For example:
    “lua-5.3.1/src”
  4. Assuming you are using C++, add this line to the top of your source to include the headers needed in a C++ friendly way:
    #include “lua.hpp”
  5. That is it!  You are all set and ready to use LUA in your program.
  6. OK, here is an example to get you started!
const char* luaScript = "\
addCount=0\n\
\n\
function addTwoNumbers(x,y)\n\
  addCount = addCount + 1\n\
  return (x+y)\n\
end\n\
\n";
int luaScriptLength = strlen(luaScript);
int addCount;
int addResult;

// create lua and load the script data
lua_State *L = 0;
L = luaL_newstate();
luaL_loadbuffer(L, luaScript, luaScriptLength, "luaScript");
lua_pcall(L, 0, 0, 0);

// get value of addCount global variable
// stack addCount
lua_getglobal(L, "addCount");
// value of top stack item (in this case addCount)
addCount = lua_tointeger(L, -1);
// addCount should be 0
// unstack var1
lua_pop(L, 1);

// add two numbers
// stack addTwoNumbers
lua_getglobal(L, "addTwoNumbers");
// stack value 5
lua_pushinteger(L, 5);
// stack value 10
lua_pushinteger(L, 10);
// call 2 stacked args, 1 stacked function, and expect 1 stack result
lua_pcall(L, 2, 1, 0);
// value of top stack item (in this case the addTwoNumbers result)
addResult = lua_tointeger(L, -1);
// addResult should be 15
// unstack result
lua_pop(L, 1);

// get value of addCount global variable
// stack addCount
lua_getglobal(L, "addCount");
// value of top stack item (in this case addCount)
addCount = lua_tointeger(L, -1);
// addCount should be 1
// unstack addCount
lua_pop(L, 1);

lua_close(L);

TI CC3200 LaunchPad Gotcha

Important things to note about the CC3200:

1.  You can get the launch pad from Digikey for about $30

2.  If you follow the Getting Started Guide, make sure you also try the following when the Code Composer connection fails:

A.  press the reset button on the board before debugging

B.  Do Not open your serial port terminal application until after the debug session has started (or it won’t start at all)

3.  Code Composer is pretty low-level and may ultimately be unnecessary.  You may get faster results using Energia