time.h functions do not see the header file - ios

I am using structures and methods listed below and including sys/time.h or time.h do not change anything. What can be the problem?
struct tm theTime;
strptime((char *)nodeValue, "%a %b %d %H:%M:%S +0000 %Y", &theTime);
time_t epochTime = timegm(&theTime);
I am using XCode and compile for armv7. (Before, this was working perfectly. I don't know what's changed since before..)
I get the following errors:
Variable has incomplete type 'struct tm'
Implicit declaration of function 'strptime' is invalid in C99
Implicit declaration of function 'timegm' is invalid in C99

Enable the declarations by defining feature test macro -D_POSIX_C_SOURCE=200809L at compilation time or before including the header:
#define _POSIX_C_SOURCE 200809L
#include <time.h>
See here for more information on feature test macro _POSIX_C_SOURCE.

I have included another header path which also includes time.h and which's content is irrelevant. I fixed it and everything works again.
So, in situations like this, it is a good idea to check header paths to see if there is any other file with the same name but in another directory.
#ouah's answer is great and it is a good tip, however it is not the direct solution of my problem.

Related

Ada library initialisation/elaboration and GPR directives : can't find elaboration symbol

I am trying to produce an Ada library for iOS.
However, it is necessary to perform the Ada elaboration manually.
I know that the compiler can produce an init symbol, that can be later imported and used. However, with the following GPR definition, it is not produced (the nm command does not list it). The naming is supposed to be <libname>init with <libname> corresponding to the value defined in the GPR directive Library_Name
The GPR is defined in the following fashion (this one is windows/style -see DLL references-, but the problems also applies when producing for iOS on a Mac):
project adalib is
for Languages use ("Ada");
for Source_Dirs use (project'Project_Dir & "./src");
for Library_Kind use "static"; --"static" on iOS will produce a .a file
for Library_Name use project'Name; -- will produce "libadalib.a"
for Library_Dir use project'Project_Dir & "./lib";
for Library_Src_Dir use project'Project_Dir & "./includes";
-- define your favorite compiler, builder, binder, linker options
end adalib;
I'm missing it : how to produce that symbol ?
I found the solution.
My GPR was missing this simple directive:
for Library_Interface use ("mypackage"); -- put whatever packages you want to expose, without .adb/.ads since we're talking about packages
With the above directive, I can find the adalibinit symbol via nm command.
When I import it in my ada code, I can also use it, see :
package body mypackage is
procedure Init_My_Lib
is
-- I want to call elaboration;
pragma import (C, ada_elaboration, "adalibinit");
begin
ada_elaboration;
-- further code
end Init_My_Lib;
-- rest of package
So, the full GPR should be:
project adalib is
for Languages use ("Ada");
for Source_Dirs use (project'Project_Dir & "./src");
for Library_Kind use "static"; -- will produce a .a file
for Library_Name use project'Name; -- will produce "libadalib.a"
for Library_Interface use ("mypackage"); -- <=== THIS IS HERE
for Library_Dir use project'Project_Dir & "./lib";
for Library_Src_Dir use project'Project_Dir & "./includes";
-- define your favorite compiler, builder, binder, linker options
end adalib;

Provide fallback symbol for dynamic linking

When dynamically linking a library is there a way to specify a fallback symbol in case one is missing at load time.
For example compiling a MEX file instead of mxCreateNumericArray I'd like to call mxCreateUninitNumericArray (with same signature). But the latter won't be present for older MATLAB versions. Same for mxArrayToString and mxArrayToUTF8String.
For MSVC I've been able to use /DELAYLOAD and hook into __pfnDliFailureHook2 on dliFailGetProc to provide a simple mapping. But what can I do on Unix based systems?
How to hook into libld in a similar way?
I've not been able to hook into the dynamic linker, but providing weak symbols helped me out like this:
extern "C"
char * __attribute__ ((weak)) mxArrayToUTF8String( mxArray const * array )
{
return mxArrayToString( array ) ;
}

Xcode 7.3: "Ambiguous expansion of macro" when re-defining macro in prefix file

I am using Xcode 7.3, and I am getting an "Ambiguous expansion of macro" warning, for a macro which was defined in Foundation, but which I have undefined and re-defined in my prefix file. I have modules enabled.
To reproduce:
Set "Enable Modules (C and Objective-C)" to Yes in build settings
Use the following prefix file:
#import <Foundation/Foundation.h>
#undef assert
#define assert(e) NSLog(#"hi") // implementation is not important
Use the following main source file:
int main() {
assert(42);
return 0;
}
Then build in Xcode.
It shows an "Ambiguous expansion of macro 'assert'" warning on the line in the source file that uses the "assert" macro. The "Expanding this definition of 'assert'" points to the definition from the system header, not my redefinition. The "Other definition of 'assert'" points to the definition in my prefix file.
This warning does not happen when modules is disabled.
This is a bug in Xcode; we'd appreciate if you could file a bug report at https://bugreport.apple.com and leave the bug # in a comment here. Your options for working around this bug in the meantime are:
You could use a different name than "assert" for this macro.
You could set the GCC_PRECOMPILE_PREFIX_HEADER build setting to NO, since PCH don’t provide a lot of benefit when you already have modules. The prefix header will still work, it just won’t be turned into a PCH.
You could turn off modules.

Read URL string from .xcconfig file in code [duplicate]

I have an xcconfig file which contains a configuration for which server my app should hit. In debug mode, this will be a different server than for release builds.
The problem I have is that a URL of the form http://www.stackoverflow.com is treated as a comment after the double slash. So the string I get in code is 'http:'
I have read that I can put a -traditional build flag on Info.plist, I was wondering if someone else has had a similar issue and has solved it?
Thanks.
Here's a simple workaround:
WEBSITE_URL = https:/$()/www.example.com
I also could not figure out how to use a double slash in a xcconfig file.
But I found a workaround in
Re: Double forward slashes in .xcconfig-defined build settings
from the Xcode-users mailing list: In the xcconfigfile, save the URL without the http scheme:
MYURL = stackoverflow.com
In the Info.plist, set the property value to
http://${MYURL}
Just declare
SIMPLE_SLASH=/
Then your URL becomes
http:$(SIMPLE_SLASH)/www.stackoverflow.com
SLASH=/
API_URL=http:$(SLASH)/endpoint.com
Another approach that improves readability could be:
PROTOCOL = http:/
API_URL = $(PROTOCOL)/www.stackoverflow.com
This way protocol can be used elsewhere
You shouldn't use a xcconfig file for this setting.
A xcconfig file is not a "normal" header or module file which is the input of the preprocessor and eventually the input for the compiler. It's nowhere specified how the xcconfig file parser treats character encoding, whether it recognizes escape sequences, whether it expands macros, and how character literals are defined and much more.
It's far better in this case, to have a "config.h" header file and use a conditional based on a preprocessor definition:
#if defined (DEBUG)
NSURL* url = ...
#else
NSURL* url = ...
#endif
Here, DEBUG is defined for Debug configuration by default. You may #define any other definition in the build settings under "Preprocessor Macros".

Calling Library in Lua

I have created a Wireshark dissector in Lua for an application over TCP. I am attempting to use zlib compression and base64 decryption. How do I actually create or call an existing c library in Lua?
The documentation I have seen just says that you can get the libraries and use either the require() call or the luaopen_ call, but not how to actually make the program find and recognize the actual library. All of this is being done in Windows.
You can't load any existing C library, which was not created for Lua, with plain Lua. It's not trivial at least.
*.so/*.dll must follow some specific standard, which is bluntly mentioned in programming in Lua#26.2 and lua-users wiki, code sample. Also similar question answered here.
There are two ways You could solve Your problem:
Writing Your own Lua zlib library wrapper, following those standards.
Taking some already finished solution:
zlib#luapower
lua-zlib
ffi
Bigger list #lua-users wiki
The same applies to base64 encoding/decoding. Only difference, there are already plain-Lua libraries for that. Code samples and couple of links #lua-users wiki.
NOTE: Lua module package managers like LuaRocks or
LuaDist MIGHT save You plenty of time.
Also, simply loading a Lua module usually consists of one line:
local zlib = require("zlib")
The module would be searched in places defined in Your Lua interpreter's luaconf.h file.
For 5.1 it's:
#if defined(_WIN32)
/*
** In Windows, any exclamation mark ('!') in the path is replaced by the
** path of the directory of the executable file of the current process.
*/
#define LUA_LDIR "!\\lua\\"
#define LUA_CDIR "!\\"
#define LUA_PATH_DEFAULT \
".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
#define LUA_CPATH_DEFAULT \
".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
#else
How do I actually create or call an existing c library in Lua?
An arbitrary library, not written for use by Lua? You generally can't.
A Lua consumable "module" must be linked against the Lua API -- the same version as the host interpreter, such as Lua5.1.dll in the root of the Wireshark directory -- and expose a C-callable function matching the lua_CFunction signature. Lua can load the library and call that function, and it's up to that function to actually expose functionality to Lua using the Lua API.
Your zlib and/or base64 libraries know nothing about Lua. If you had a Lua interpreter with a built-in FFI, or you found a FFI Lua module you could load, you could probably get this to work, but it's really more trouble than it's worth. Writing a Lua module is actually super easy, and you can tailor the interface to be more idiomatic for Lua.
I don't have zlib or a base64 C library handy, so for example's sake lets say we wanted to let our Lua script use the MessageBox function from the user32.dll library in Windows.
#include <windows.h>
#include "lauxlib.h"
static int luaMessageBox (lua_State* L) {
const char* message = luaL_checkstring(L,1);
MessageBox(NULL, message, "", MB_OK);
return 0;
}
int __declspec(dllexport) __cdecl luaopen_messagebox (lua_State* L) {
lua_register(L, "msgbox", luaMessageBox);
return 0;
}
To build this, we need to link against user32.dll (contains MessageBox) and lua5.1.dll (contains the Lua API). You can get Lua5.1.lib from the Wireshark source. Here's using Microsoft's compiler to produce messagebox.dll:
cl /LD /Ilua-5.1.4/src messagebox.c user32.lib lua5.1.lib
Now your Lua scripts can write:
require "messagebox"
msgbox("Hello, World!")
Your only option is to use a library library like alien. See my answer Disabling Desktop Composition using Lua Scripting for other FFI libraries.

Resources