Include .a iOS Library in C++ with Embarcadero Rad Studio - c++

I am trying to include a little static library (.a) containing a simple sum function that I compiled in X-Code for iOS into Embarcadero Rad Studio C++
The closest post I found in the following link but it does not seem to be working as the compiler raises me an error.
Using Static & Dynamic Libraries In Embarcadero C++ Builder
Here is my little C code compiled in X-Code:
int c_sum (int a, int b){
return a+b;
}
The c_lib.h is
int c_sum (int a, int b);
The compiled version is c_lib.a
I included the .a in the Rad Studio Project and
#include "c_lib.h"
When I try to call the function the Editor shows up the function prototype but then I compile it is comes out with this error:
[ld Error] Error: "__Z5c_sumii", referenced from:
__ZN6TForm112Button1ClickEPN6System7TObjectE in .\iOSDevice64\Debug\Unit1.o;
I tried also to include it with pragma #pragma comment(lib, "c_lib.a") same problem.
The purpose of including external libraries is to write specific code in X-Code rather than wrap functions in Embarcadero and use them directly as external static libraries which will be included in my final app.
The second step will be calling methods in objects such as alloc and init.
Can anybody help?!

Related

Use NetCDF4-C in Unity

this is my first time posting (after lurking for years).
A project I will be tackling is to use NetCDF4 (.nc) files in Unity on Windows. I will be using Unity 5.4.0f3 and Windows 10, and I have developed in Unity before and am familiar with C# and JavaScript, but NetCDF only has C, Java, and Fortran APIs, although there are wrappers in Python, C++, and others (source: https://www.unidata.ucar.edu/publications/factsheets/current/factsheet_netcdf.pdf).
So my specific question is how do I call NetCDF4-C functions (nc_get_vara_float(), nc_open, etc) in C# for use in Unity?
What I've tried so far:
To start, I googled specifically for NetCDF4-C + Unity tutorials/attempts, but did not find anything, so instead I have been looking into the compatibility of calling C functions from C#. I am currently working on a project on Linux with NetCDF4-C and written custom wrapper functions for the netcdf.h functions, so I was hoping I could reuse my code there.
I attempted to follow this SO post (Is it possible to call a C function from C#.Net) but get an error in Unity when trying to Play: "DllNotFoundException: test.so" (my file was named "test.c"). From the comments, it seems Linux uses .so files but Windows uses .dll, and I was not sure how to generate a .dll of a C file.
I looked up another post on that (How to write a DLL file in C?) and downloaded Visual Studio to follow along. While VS was downloading, I looked up how to use GCC to compile (Creating a DLL in GCC or Cygwin?), and used the Bash subsystem ("Bash on Ubuntu on Windows" terminal) but got a handful of errors that indicated the code from the previous link (2nd SO link in this post) were for C++, so I stopped working with GCC.
Once VS finished installing, I went back to trying to use VS to create the .dll, and attempted to combine the solutions from both SO posts (1 and 2) so that I would be able to use the .dll file containing C code in Unity, but to no avail: I get the same error but just with a different extension (and different name on purpose): "DllNotFoundException: Win32Project1.dll".
The code I have is as follows:
test.cs (used in Unity and attaches to a Component):
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class test : MonoBehaviour {
[DllImport("Win32Project1.dll", EntryPoint="DisplayHelloFromMyDLL")]
public static extern void DisplayHelloFromMyDLL ();
// Use this for initialization
void Start () {
DisplayHelloFromMyDLL();
}
// Update is called once per frame
void Update () {
}
}
Win32Project1.dll (created and built in Visual Studio):
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromMyDLL()
{
printf("Hello DLL.\n");
}
}

Link a MSVC compiled DLL in a MinGW-built project

Given two C++ projects:
a Win32 C++ project compiled as a x86 DLL under Visual Studio 2015
a Qt GUI application using Desktop Qt 5.5.1 MinGW 32 bit Kit.
What I'm trying to do is to link the first one in the second one. A MWE follows.
DLL header file: libxspectra.h
namespace XSpectra
{
#define LIBXSPECTRA_EXPORTS // already defined into Project Properties
#ifdef LIBXSPECTRA_EXPORTS
#define LIBXSPECTRA_API __declspec(dllexport)
#else
#define LIBXSPECTRA_API __declspec(dllimport)
#endif
LIBXSPECTRA_API int fnlibxspectra(void);
LIBXSPECTRA_API int gnara(void) { return 7; };
int foo() { return 1; };
int bar();
}
DLL source file: libxspectra.cpp
#include "libxspectra.h"
namespace XSpectra
{
LIBXSPECTRA_API int fnlibxspectra(void)
{
return 42;
}
int bar()
{
return 6;
}
}
Qt source file: main.cpp
#include "libxspectra.h"
int main(int argc, char *argv[])
{
XSpectra::foo();
XSpectra::bar();
XSpectra::gnara();
XSpectra::fnlibxspectra();
return 0;
}
Qt application build log
error: undefined reference to XSpectra::bar()
error: undefined reference to _imp___ZN8XSpectra13fnlibxspectraEv
While foo() and gnara() links correctly.
A few notes
I know the problem is not strictly Qt-related, but it's a matter of different compilation toolchains, a field where I'm a real novice. I'm actually asking for advices on this way.
If I comment the #define LIBXSPECTRA_EXPORTS, Visual Studio's Intellisense still marks it as defined, dll compiles, but the behaviour of external application's build process changes. The following error arises:
error: function 'int XSpectra::gnara()' definition is marked dllimport
You can only link MSVC compiled C DLLs with MinGW, and only on 32-bit Windows. The MinGW linker can link directly to the DLL (if the functions are properly exported and not only available through an import library) or the usual import library. See here and here for how to generate a MinGW import library from a DLL.
You'll do it just like with MSVC (compile the dll with the functions marked dllexport, and compile the code using the dll with the functions marked dllimport, or use a .def file or something). Remember you need to export C functions, which means they need to be marked extern "C".
I would strongly suggest though, making the code compatible with MinGW, and just compile everything with that. Or use the MSVC version of Qt.

how to make armadillo-5.200.1 (+openblas or lapacke) work with visual studio 2010?

I have been trying to make armadillo-5.200.1 and openblas (or lapacke) work with visual studio 2010 since a week now and I still have several unresolved external symbols errors with functions from the blas and lapack libraries.
First, I tried to install openblas from pre-built binaries for windows and set up a new project and test it with the sample code below:
#include <cblas.h>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
blasint n = 10;
blasint in_x =1;
blasint in_y =1;
std::vector<double> x(n);
std::vector<double> y(n);
double alpha = 10;
std::fill(x.begin(),x.end(),1.0);
std::fill(y.begin(),y.end(),2.0);
cblas_daxpy( n, alpha, &x[0], in_x, &y[0], in_y);
//Print y
for(int j=0;j<n;j++)
std::cout << y[j] << "\t";
std::cout << std::endl;
}
I got an unresolved external symbol « cblas_daxpy » error. I tried to build the same project with code::blocks and got the same error.
I downloaded and set up msys to be able to compile the source I downloaded on github (https://github.com/xianyi/Openblas) and tried to make it work both on code::blocks and visual studio, but I still got the same errors.
Then I tried on my own laptop, because it has Linux ubuntu 14.04 LTS. So, I cloned the github and compiled the sources and tried to set it up in another code::blocks project and I test it using the same sample code and got undefined reference errors concerning functions such as pthread_create and pthread_join.
I also tried to compile lapacke sources with cmake, following the instructions of the following page : https://icl.cs.utk.edu/lapack-for-windows/lapack/. I used the example, to test, the project built correctly, but when I execute the programm I have an pop-up window message saying that the liblapacke.dll can't be find.
To use openblas with visual studio, I followed the instructions about third-party library:
add the location of the header files to "C/C++->Additional Include Directories"
add the .lib files to the "Linker->Input->Additional Dependencies"
add the location of the .lib files to "Linker->General->Additional Library Directories"
I need to make this work at least with windows 7 and better with visual studio 2010, because we use it at work.
you should copy dll file to system32 direction or Debug or Release folder of y

How to build and use a C++ static library for ios application

I know how to build a object C static library using iOS->Framework&Library->Cocoa Touch Static Library in xcode 4.6, and it is straightforward with the help of this tutorial Creating a Static Library in iOS Tutorial. One thing I am not sure, however, is how to build and use a pure C++ static library for io application. For building a C++ static library, I am also use iOS->Framework&Library->Cocoa Touch Static Library guideline, and the difference is that I delete all the .h and .m files when creating the static library project and then put all the C++ static library head files and implementation files in the project. A very simple example is as follows:
hello.h
#include <iostream>
void say_hello();
hello.cpp
#include "hello.h"
void say_hello()
{
std::cout<<"hello"<<std::endl;
}
It seems working, and I can build hello.a static library for iPhone 6.1 Simulator. The next step is to build an application that will invoke the static library. I build a simple iOS application->Single View Application for iPhone 6.1 Simulator, and then try to invoke the hello.a static library in ViewController.mm file (change ViewController.m to ViewController.mm so that it can invoke C++ function) simply with the following code:
say_hello();
However, I received one warning and two error messages:
Warning:
ld: warning: ignoring file hello.a, file was built for archive which is not the architecture being linked (i386):
Error 1:
hello.a
Undefined symbols for architecture i386:
"say_hello()", referenced from:
-[ViewController viewDidLoad] in ViewController.o
Error 2:
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I then have several questions related to this experiment:
Is it the right way to create a pure C++ static library?
Is there something wrong with the way how I invoke the C++ static
library?
In my example, when invoking the static library, how could I solve the link errors?
Many thanks.
This will do,
1)Create c++ library using same way, iOS->Framework&Library->Cocoa Touch Static Library in Xcode 6.
TestCPlusPlus.h
int sub(int a, int b);
TestCPlusPlus.cpp
int sub(int a, int b)
{
return a - b;
}
2) Build the static library keeping configuration iOS Device, then iPhone 6(Basically simulator.)
3) then Expand Products in File Browser view. Select your .a file, say libTestStaticLibrary.a , then Right Button > Show in Finder. Move up in folders. You should be able to see two folers Debug-iphoneos and Debug-iphonesimulator
4) now
open Terminal go till this Products path then type
lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a -output libTestStaticLibrary.a
5) Now open your project which uses this library, you need to drag and drop the static library files as well as the header files which have function declaration of static library functions.
6) Now, create Cocoa touch class file which will act as adaptor between static library and obejective -c files. Change the extension to .mm
MyCustomAdaptor.h
#interface MyCustomAdaptor : NSObject
-(int)getSub:(int ) a SecondParam:(int) b;
#end
MyCustomAdaptor.mm
#import "TestCPlusPlus.h"
#implementation MyCustomAdaptor
-(int)getSub:(int ) a SecondParam:(int) b
{
int c = sub(a,b);
return c;
}
#end
7) now use this MyCustomAdaptor in any of the objective c- file.
Please notice that your .a is build with i386 or armv7?
Generally, you should build both versions and combine them into one.
like this:
lipo -create -output libopencore-amrwb.a libopencore-amrwb-armv7.a libopencore-amrwb-i386.a
I'm currently doing the same as you. I've had the same problem you describe here, actually the same two errors.
When you build your library you have to have in mind where're you going to use it, iOS device or simulator.
This is important because you have to build for the different cases, this is very simple, when you build you library just check the "Select Scheme".
For Real device use:
Just to test in the simulator use:
After building just drag-drop the files created to the project you want to use the library and you're good to go!

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console