2021年5月23日日曜日

共有ライブラリをつかって https://taiyakisun.hatenablog.com/entry/20150506/1430896155

 C/C++でのお話です。

忘れがちなのでまとめておきます。

備忘録

共有ライブラリ作成時
  • VC++ではexportする関数毎に__declspec(dllexport)が必要になるが、gccの場合はstaticが付与されていなければ自動的に公開になるため不要
  • 共有ライブラリ(拡張子so)のファイル名には先頭に必ずlibをつける(例:sharedlib.cならlibsharedlib.so)
  • -fPIC」(Position Independent Code)オプションをつけてコンパイルした方がよい
共有ライブラリ使用時
  • 先頭のlibと拡張子.soを除いた名前を指定する(例:libsharedlib.soなら-l sharedlib)
  • ヘッダファイルやライブラリパスの場所を指定する-Iや-Lはスペースを空けずにオプションの直後にパスを書く(例:-I/home/sample -L./lib)

共有ライブラリの作成

#include <stdio.h>

int add( int n1, int n2 )
{
    printf( "result:%d + %d = %d\n", n1, n2, n1+n2 );
    return n1+n2;
}
gcc -shared -fPIC -o libsharedlib.so sharedlib.c

共有ライブラリの使用

#include <stdio.h>

int main()
{
  int n1 = 10;
  int n2 = 20;

  printf( "caller:%d + %d = %d\n", n1, n2, add(n1,n2) );

  return 0;
}
gcc -I./ -L./ -o main main.c -lsharedlib

正しく実行されれば以下が標準出力に出力されます。

result:10 + 20 = 30
caller:10 + 20 = 30

もし共有ファイル名の先頭が「lib」でない場合は以下のように
共有ライブラリが見つけられずリンカーエラーになってしまいます。

/usr/bin/ld: cannot find -lsharedlib

実行ファイル実行時

ライブラリのパスが通っている場所に共有ライブラリを置くか、
もしくは以下のように環境変数LD_LIBRARY_PATHにパスを指定してから実行します。

自注)この設定は、恒久的でなくシェルを閉じるとなくなる!

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./

これを実施しないと以下のように共有ライブラリが
見つからない旨のエラーメッセージが出力されます。

./main: error while loading shared libraries: libsharedlib.so: cannot open shared object file: No such file or directory

2021年5月7日金曜日

tuyano flutter 326 page(file access) and 316 page (routing)

ファイルIOだから、Webでは実現できない(ファイル書き込みできないから)

よってアンドロイドスタヂオでエミュレータ実施で成功した


 iimport 'package:flutter/material.dart';


import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
},
);
}
}

class FirstScreen extends StatefulWidget {
FirstScreen({Key key}) : super(key: key);

@override
_FirstScreenState createState() => new _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
final _controller = TextEditingController();
final _fname = 'mydata.txt';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Column(
children: <Widget>[
Text(
'Home Screen',
style: TextStyle(fontSize: 32.0),
),
Padding(
padding: EdgeInsets.all(20.0),
),
TextField(
controller: _controller,
style: TextStyle(fontSize: 32.0),
)
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('Save'),
icon: Icon(Icons.save),
),
BottomNavigationBarItem(
title: Text('Load'),
icon: Icon(Icons.open_in_new),
),
],
onTap: (int value) {
switch (value) {
case 0:
saveIt(_controller.text);
setState(() {
_controller.text = '';
});
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("saved!"),
content: Text("save message to file."),
));
break;
case 1:
setState(() {
loadIt().then((String value) {
setState(() {
_controller.text = value;
});
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("loaded!"),
content: Text("load message from file."),
));
});
});
break;
default:
print('no defalut.');
}
},
),
);
}

Future<File> getDataFile(String filename) async {
final directory = await getApplicationDocumentsDirectory();
return File(directory.path + '/' + filename);
}

void saveIt(String value) async {
getDataFile(_fname).then((File file) {
file.writeAsString(value);
});
}

Future<String> loadIt() async {
try {
final file = await getDataFile(_fname);
return file.readAsString();
} catch (e) {
return null;
}
}
}


----------------------------------------------------------

mport 'package:flutter/material.dart';


void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: new FirstScreen(),
);
}
}

class FirstScreen extends StatefulWidget {
FirstScreen({Key key}) : super(key: key); // コンストラクタ。今回は特に処理はない

@override
_FirstScreenState createState() => new _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
final _controller = TextEditingController();
String _input;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Home Screen', style: TextStyle(fontSize: 32.0)),
Padding(
padding: EdgeInsets.all(10.0),
),
TextField(
controller: _controller,
style: TextStyle(fontSize: 32.0),
onChanged: changeField,
),
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 1,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('Home'),
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
title: Text('next'),
icon: Icon(Icons.navigate_next),
),
],
onTap: (int value) {
if (value == 1)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen(_input)),
);
},
),
);
}

void changeField(String val) => _input = val;
}

class SecondScreen extends StatelessWidget {
final String _value;

SecondScreen(this._value);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Next"),
),
body: Center(
child: Text('you typed: "$_value".', style: TextStyle(fontSize: 32.0)),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('prev'),
icon: Icon(Icons.navigate_before),
),
BottomNavigationBarItem(
title: Text('?'),
icon: Icon(Icons.android),
),
],
onTap: (int value) {
if (value == 0) Navigator.pop(context);
},
),
);
}
}