Re: Building with dub fails on Ubuntu 16.10.

2016-12-05 Thread moe via Digitalmars-d-learn

On Monday, 5 December 2016 at 07:32:21 UTC, Daniel Kozak wrote:

On Saturday, 3 December 2016 at 16:07:47 UTC, moe wrote:
On Sunday, 11 September 2016 at 02:17:21 UTC, Vlasov Roman 
wrote:

Hello, guys.
I tried to build HelloWorld with dub, but i got strange 
linker error:


[...]



I just switched from Windows to linux (arch) and got the exact 
same problem. Did you resolve this yet? I'm not very 
experienced with development on linux any hint here would be 
welcome.


On arch linux there should not be any issue. Archlinux does not 
use fPIC by default so, this error seems like you have 
something wrong with your Archlinux installation.


Thanks for the tip. I haven't done a lot of development on linux 
so I messed quite a bit with my arch install. It's very well 
possible that I messed something up. I am planing to reinstall in 
a few weeks anyway :)


Having said that, from the arch wiki: 
https://wiki.archlinux.org/index.php/D_(programming_language)
Looks like there is a hardening-wrapper which forces PIC by 
default. As clang worked just fine I assumed that was the actual 
problem. I will look into it again once I reinstalled my system.


Re: Building with dub fails on Ubuntu 16.10.

2016-12-03 Thread moe via Digitalmars-d-learn

On Saturday, 3 December 2016 at 16:07:47 UTC, moe wrote:
On Sunday, 11 September 2016 at 02:17:21 UTC, Vlasov Roman 
wrote:

Hello, guys.
I tried to build HelloWorld with dub, but i got strange linker 
error:


[...]



I just switched from Windows to linux (arch) and got the exact 
same problem. Did you resolve this yet? I'm not very 
experienced with development on linux any hint here would be 
welcome.


Never mind, I just found in the arch-wiki that on arch dmd and 
libphobos are build without PIC support. Which seams to be 
causing the problem. The easiest way is to compile using clang 
instead.


CC=/usr/bin/clang dub

Alternatively get gdc with PIC support.

Sorry for the necromancy. I have been searching for a solution 
for a couple hours. Did not expect to find something shortly 
after posting...


Re: Building with dub fails on Ubuntu 16.10.

2016-12-03 Thread moe via Digitalmars-d-learn

On Sunday, 11 September 2016 at 02:17:21 UTC, Vlasov Roman wrote:

Hello, guys.
I tried to build HelloWorld with dub, but i got strange linker 
error:


[...]



I just switched from Windows to linux (arch) and got the exact 
same problem. Did you resolve this yet? I'm not very experienced 
with development on linux any hint here would be welcome.


Re: Using .lib and .dll in D applications

2016-06-22 Thread moe via Digitalmars-d-learn

On Wednesday, 22 June 2016 at 05:34:33 UTC, Mike Parker wrote:

On Wednesday, 22 June 2016 at 03:06:29 UTC, moe wrote:


I meant like this:

- PluginContract // not a dub project, just some folder
-- iplugin.d

- TestApp // all files for the app (separate project)
-- packages
 DerelictUtil-master // contains the project for derelict
-- source
 app.d // the app
-- dub.json // the project file for the app

The only dub project would be TestApp. PluginContract would 
just be some folder completely outside the TestApp dub 
project. I could not get a relative path to work like this.


Just to be clear, are you compiling iplugin.d as well? I 
assumed you were referring to a compiler error (i.e. missing 
import), but based on this post I would guess you're getting a 
linker error. You should probably add this to your dub.json in 
addition to the importPaths:


"sourceFiles": ["../PluginContract/iplugin.d"]




I have added all of these to the dub.json:

"sourcePaths": ["../PluginContract"],
"importPaths": ["../PluginContract"],
"sourceFiles": ["../PluginContract/iplugin.d"],

In my app I use:
import iplugin;

I would expect that both the compiler and the linker finds the 
needed files. I would also prefer a path to link a folder rather 
than adding files individually. It seams more error prone when I 
have to remember to add every file in a bigger project. However, 
every combination of the above seams to fail. With a linker error.


Linking...
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 23: No Stack
.dub\build\debug-debug-windows-x86-dmd_2071-0BEC1C92408DC77EE5C50BCF4B1225A9\tes
tapp.obj(testapp)
 Error 42: Symbol Undefined _D18TypeInfo_Interface6__vtblZ
.dub\build\debug-debug-windows-x86-dmd_2071-0BEC1C92408DC77EE5C50BCF4B1225A9\tes
tapp.obj(testapp)
 Error 42: Symbol Undefined _D14TypeInfo_Class6__vtblZ
OPTLINK : Warning 134: No Start Address
--- errorlevel 2
dmd failed with exit code 2.


Without the adjustments in the dub.json I get the following error 
(But that is expected if dub only searches in the source folder 
by default):


testplugin ~master: building configuration "debug"...
source\SomePlugin.d(3,8): Error: module iplugin is in file 
'iplugin.d' which can

not be read
import path[0] = source
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
dmd failed with exit code 1.


Re: Using .lib and .dll in D applications

2016-06-21 Thread moe via Digitalmars-d-learn

On Wednesday, 22 June 2016 at 01:40:47 UTC, Mike Parker wrote:

On Tuesday, 21 June 2016 at 23:59:54 UTC, moe wrote:
I had some time to try it out and I finally got it to work. I 
have only tried in windows so far but there was a pitfall in 
windows. Your dll need a DllMain entry to compile. This was 
the only thing that was missing from your information.


Right, I forgot about that. It's always optional in C and C++, 
but in D we need to use it to initialize the runtime.




-- TestApp // all files for the app (separate project)
 packages
-- DerelictUtil-master // contains the project for derelict
 source
-- app.d // the app
 dub.json // the project file for the app


This is not the way DerelictUtil, or any DUB package, is 
intended to be used when you use DUB to manage your project. 
You don't need to download it this way. You chould change your 
dependency in TestApp:




"dependencies": {
"derelict-util":  "version=~>2.0.6",


2.0.6 is the latest. The '~>' means >=2.0.6 && <2.1.0, so that 
if there is a 2.0.7 released, you can run 'dub upgrade' on your 
project and start using it. The same for 2.0.8, 2.0.9, 2.0.n...


Of course, if you have a reason for doing it that way, more 
power to you :)


Yes, I did it intentionally. I wanted to ensure that the packages 
are self contained and check whether it would work fine like 
this. Basically I like to have a project that contains everything 
it needs with the versions originally used to build. It sort of 
creates a reference for learning.


For testing I would have also liked not to build a dub project 
for the PluginContract but rather have a simple iplugins.d file 
in a folder. I could not get it to be imported if it's outside of 
a dub project.


"importPaths": ["../PluginContract"]

Did not work for some reason. But that's a minor issue.


Re: Using .lib and .dll in D applications

2016-06-21 Thread moe via Digitalmars-d-learn
I had some time to try it out and I finally got it to work. I 
have only tried in windows so far but there was a pitfall in 
windows. Your dll need a DllMain entry to compile. This was the 
only thing that was missing from your information. The rest 
worked perfectly. This may be obvious to most around here, but I 
did not know before. So, I thought it might make sense to show my 
working solution in case someone else stumbles upon the same 
problem.


I wanted the app and the plugin to be independent projects form 
one another. So they both need a shared project containing the 
interface for the plugin. That makes the 3 projects shown below. 
I can than after compiling the app simply copy the plugin dll 
into a plugins folder and the app will find it on demand. So that 
I could later add more plugins if desired. Obviously in that case 
I would have to get a list of available plugins by reading the 
filenames in the plugins directory instead of a hard coded path 
in the app. I tried to reduce it to the bare minimum with a 
working solution. Even with the DllMain entry (which is necessary 
for windows) it turns out to be surprisingly compact.


There are 3 distinct projects. I have the following directories 
(omitting some generated by dub):


- PluginTest

-- PluginContract // all files for the shared plugin interface 
(separate project)

 source
-- iplugin.d // the interface for the plugin
 dub.json // the project file for the shared plugin interface

-- TestApp // all files for the app (separate project)
 packages
-- DerelictUtil-master // contains the project for derelict
 source
-- app.d // the app
 dub.json // the project file for the app

-- TestPlugin // all files for the plugin (separate project)
 source
-- someplugin.d // the plugin
 dub.json // the project file for the plugin


Here are the files:

Shared plugin interface (Project 1)
===
Note, these are necessary for the linker to find the files:
"targetType": "library"
"targetPath": "lib"

PluginTest/PluginContract/dub.json
--
{
"name": "plugincontract",
"authors": ["root"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "library",
"configurations": [
{
"name": "debug",
"targetPath": "lib",
"buildOptions": ["debugMode", "debugInfo"],
},
{
"name": "release",
"targetPath": "lib",
"buildOptions": ["releaseMode", "optimize", "inline"],
}
]
}


PluginTest/PluginContract/source/iplugin.d
--
module iplugin;

interface IPlugin
{
void Talk(string msg);
}





TestApp (Project 2)
===

PluginTest/TestApp/dub.json
---
{
"name": "testapp",
"authors": ["root"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "executable",
"dependencies": {
"derelict-util":  {"path": "packages/DerelictUtil-master"},
"plugincontract": {"path": "../PluginContract"}
},
"configurations": [
{
"name": "debug",
"targetPath": "bin/debug",
"buildOptions": ["debugMode", "debugInfo"],
},
{
"name": "release",
"targetPath": "bin/release",
"buildOptions": ["releaseMode", "optimize", "inline"],
}
]
}


PluginTest/TestApp/source/app.d
---
import std.stdio;
import derelict.util.sharedlib;
import iplugin;

alias GetPluginImpl = extern(C) nothrow IPlugin function();
GetPluginImpl getPlugin;

void main()
{
SharedLib lib;
lib.load(["plugins/testplugin.dll"]);
getPlugin = cast(GetPluginImpl)lib.loadSymbol("getPlugin");

auto plugin = getPlugin();
plugin.Talk("Hello World.");

writeln("End of app.");
}




TestPlugin (Project 3)
==

PluginTest/TestPlugin/dub.json
--
{
"name": "testplugin",
"authors": ["root"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "dynamicLibrary",
"importPaths": ["../PluginContract"],
"dependencies": {
"plugincontract": {"path": "../PluginContract"}
},
"configurations": [
{
"name": 

Re: Using .lib and .dll in D applications

2016-06-20 Thread moe via Digitalmars-d-learn

On Monday, 20 June 2016 at 13:51:15 UTC, Mike Parker wrote:

interface Plugin {
   bool initialize();
   void terminate();
   Throwable getLastException();
   SomeObject getSomeObject();
   void returnSomeObject(SomeObject);
}


Sorry, I forgot a couple of commments. I did explain it in the 
text, though. It was supposed to read:


interface Plugin {
   bool initialize();
   void terminate();

   // Dont' do this! Allocate exceptions on app side.
   Throwable getLastException();

   // Do do this. Allocate plugin objects on DLL side.
   SomeObject getSomeObject();
   void returnSomeObject(SomeObject);
}


Wow, absolutely awesome! That's exactly what I have been looking 
for. I should have some time to try it out later this week. Many 
thanks for all the info. I very much appreciate that you took the 
time to explain everything. It helped me a lot!


Also thanks to everybody else for the input!


Re: Using .lib and .dll in D applications

2016-06-20 Thread moe via Digitalmars-d-learn

Thanks everyone for the info. It is very much appreciated!

What works for me right now is dealing with various packages (dub 
packages) and use them in projects. I previously made too many 
assumptions about how lib's and dll's work. It's much clearer now.


Where I still have a problem is with a plugin system. I would 
like to write an app that is plugin based. So that I can write a 
plugin to extend the functionality of the app. I imagine that I 
could copy the plugin into a folder from the (compiled) app and 
the app would be able to use the plugin without me having to 
recompile the app. I have been looking at LoadLibrary 
(https://dlang.org/library/core/runtime/runtime.load_library.html) and Object.factory (https://dlang.org/library/object/object.factory.html) but I don't quite get how to use them. I did find some info that was several years old. They mentioned that these functions are not yet ready for all platforms. I don't know if that has changed in the meantime. It is not clear from the documentation.


I would like to find some snippets or examples but that sort of 
resource for D is very hard to find online. Most of the time 
there are only some partial snippets where it turns out they only 
work under specific conditions or are restricted to one platform. 
Does D not supply some infrastructure for this kind of a task? It 
seams to me that is a very common approach to build an app. I 
expected there would be some out of the box solution for this in 
D. I would write the app and the plugins purely in D (no C 
bindings if possible).


I have tried to build a mini app which defines an interface for 
the plugins. The plugins then implement that interface. But when 
I want to load the plugin both LoadLibrary() and Object.factory() 
fail (I am aware that Object.factory() requires the full 
qualified class names).


Is there a recommended way of writing a simple plugin system that 
works on windows and linux? Maybe someone has a working example 
or knows a book that covers how to write a plugin system?


Re: Using .lib and .dll in D applications

2016-06-19 Thread moe via Digitalmars-d-learn

On Sunday, 19 June 2016 at 18:00:07 UTC, Mike Parker wrote:

On Sunday, 19 June 2016 at 17:33:43 UTC, moe wrote:







Unfortunatelly I still don't get it. I would like to have an 
independant project "dbar". The created lib is then used in 
another project "dfoo". Assuming that "dfoo" has no access to 
"dbar" other than the .lib file.


You can't do it with only the lib file. You *need* the source 
file too for the import statement. As I explained, the lib file 
is used by the linker, not the compiler. The compiler needs the 
source file.




My folder structure is like this:

-dtest
--dbar
source\barlib.d
dub.json
This project creates a dbar.lib file which seams to work.


-dtest
--dfoo
lib\dbar.d  // copied from the dbar project
source\app.d
dub.json


You don't need to copy dbar to the lib directory in this case.

This project would use the dbar.lib but should otherwise not 
have access to the dbar project. Basically simulating, that 
someone else made a dbar project to which I would not have 
access other than using the dbar.lib. How do I have to 
configure the dub.json file for this to work?


One of two things would happen:

1) They would register the project with he dub registry, then 
you add a dependency to a specific version the library. Dub 
would then download the necessary files for you and ensure that 
everything you need is passed to the compiler when building 
your project.


2) They would provide some other means for you to get the 
source and the library. Then you would need to manually 
configure your dub.json to pass the import path to the compiler 
and link with the library.




I have tried a variety of configurations for the dub.json. At 
this point it feels like a bad guessing game. That is no way 
to deveop anything. I need to figure out how to properly setup 
the dub.json but I don't seam to find the answer online. 
"http://code.dlang.org/package-format?lang=json; isn't very 
helpful.


All the information you need is there on that page.



I have meanwhile adjusted my dtest/dfoo/dub.json to this:



"dependencies": {
"dbar": "~master"
}


This gives me the error: "Root package dfoo references unknown 
package dear"


As I explained above, you need a path attribute for the 
dependency in this case since it is on your local file system 
and not in the registry. The documentation link I gave you 
explains how to to this. Try this:


"dependencies": {
"dbar":  {"path": "../dbar"}
}


I see where I went wrong. I thought that it's possible to only 
use the .lib file without the source code of dbar. Having access 
to the source makes what I am trying somewhat pointless. Is it 
otherwise possible to provide some functionality without having 
to give away your source? I would like to put together a library 
that I can reuse, without having to rely on the source each time. 
Maybe a dll instead?


Note: I don't have a problem with giving away my code. I just 
want to know if it can be done. Or maybe later build a plugin 
system where the creator of the plugin does not need the source 
of the entire application. And in turn the app does not need to 
be recompiled in order to use the plugin.


Thanks for your help!


Re: Using .lib and .dll in D applications

2016-06-19 Thread moe via Digitalmars-d-learn

On Sunday, 19 June 2016 at 17:33:43 UTC, moe wrote:

On Sunday, 19 June 2016 at 16:31:40 UTC, Mike Parker wrote:

[...]


Thanks for the reply.

Unfortunatelly I still don't get it. I would like to have an 
independant project "dbar". The created lib is then used in 
another project "dfoo". Assuming that "dfoo" has no access to 
"dbar" other than the .lib file.


[...]


Darn, I can't seam to edit the post. The line:
"lib\dbar.d  // copied from the dbar project"

should of course read:
lib\dbar.lib  // copied from the dbar project

I am copying the lib file. Not the .d file.


Re: Using .lib and .dll in D applications

2016-06-19 Thread moe via Digitalmars-d-learn

On Sunday, 19 June 2016 at 16:31:40 UTC, Mike Parker wrote:

On Sunday, 19 June 2016 at 15:35:04 UTC, moe wrote:

I am new to d and doing some small test apps at the moment to 
learn d. Currently I must be missing something basic here. I 
have installed dub as a project manager and I am trying to use 
a .lib file in an app. However, I can not use a module from 
the .lib file. I get the following error:


"module barlib is in file 'barlib.d' which cannot be read"


This is a compile time error, not a link time error. Imports 
modules and linking to libraries are two different things.




the dub.json for the app

{
"name": "dfoo",
"authors": ["moe"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "executable",
"configurations": [
{
"name": "debug",
"targetPath": "bin/debug",
"buildOptions": ["debugMode", "debugInfo"]
},
{
"name": "release",
"targetPath": "bin/release",
"buildOptions": ["releaseMode", "optimize", "inline"]
}
]
}



Nowhere have you set up barlib as a dependency for your app, so 
DUB has no idea it's supposed to tell dmd that you need to link 
to the library or where to find the modules. So the easy thing 
to do is to add a dependency directive. Since your library 
isn't registered with the dub registry, you'll need to provide 
a path to the barlib dub project rather than a release tag for 
the library.


Assuming a directory tree like so:
-projects
--barlib
---dub.json
--dfoo
---dub.json

Then the path for dfoo's depencency is ../barlib. With a 
dub.sdl file, it would look like this:


dependency "barlib" path="../barlib"

You can find the equivalent JSON syntax somewhere at [1]. With 
this, dub will manage your import path and library dependency 
for you.


I can successfully build the barlib.lib file but I can not use 
it in the app. I thought that it should be possible to simply 
import the module from the lib-file, but the app cannot find 
the module. I would like to be able to build .lib and .dll 
files and use them in other applications. Can someone tell me 
what I am missing?


Compiling and linking are two distinct steps. The compiler 
passes any libraries you give it to the linker, but it does not 
do anything with them itself during the compile step. At that 
stage, it's only concerned with source modules. So you need to 
tell it: where to find the source modules (on the command 
line), which ones to import (with the import statement). It 
also needs to know which libraries to pass to the linker and 
where they can be found.


Give the directory tree above, when compiling dfoo manually, 
you might do this on Windows:


cd dfoo
dmd -I../barlib/source source/app.d ../barlib/bin/barlib.lib

On other systems:

dmd -I../barlib/source -L-L../barlib/bin -L-lbarlib source/app.d

The -I switch tells the compiler where it can find the modules 
you need to import. DRuntime and Phobos are configured in DMD's 
config files, so you don't need to every specify those. On the 
windows command line, you can pass the full path to the library 
directly and the compiler will do the right thing. This is 
easier than dealing with the different syntax the two supported 
Windows linkers use for command line switches. On Posix 
systems, it's better to use the -L switch, which tell the 
compiler the immediately following command should be passed to 
the system linker. Ex:


-L-L/some/path -> passes -L/some/path to the linker, which is 
the switch telling it where to look for libraries.
-L-lFoo (-L-l -- the second letter is a lowercase 'L') -> 
passes -lname to the linker, telling it which library to link 
with.


Given this, the linker will look for /some/path/libFoo.so or 
/some/path/libFoo.a, in addition to searching the normal search 
paths.


As you can see, it's easier to properly configure your dub.json 
dependecies so that DUB can handle all of this for you.


[1] http://code.dlang.org/package-format?lang=json


Thanks for the reply.

Unfortunatelly I still don't get it. I would like to have an 
independant project "dbar". The created lib is then used in 
another project "dfoo". Assuming that "dfoo" has no access to 
"dbar" other than the .lib file.


My folder structure is like this:

-dtest
--dbar
source\barlib.d
dub.json
This project creates a dbar.lib file which seams to work.


-dtest
--dfoo
lib\dbar.d  // copied from the dbar project
source\app.d
dub.json
This project would use the dbar.lib but should otherwise not have 
access to the dbar project. Basically simulating, that someone 
else made a dbar project to which I would not have access other 
than using the dbar.lib. How do I have to configure the dub.json 
file 

Using .lib and .dll in D applications

2016-06-19 Thread moe via Digitalmars-d-learn

Hello,

I am new to d and doing some small test apps at the moment to 
learn d. Currently I must be missing something basic here. I have 
installed dub as a project manager and I am trying to use a .lib 
file in an app. However, I can not use a module from the .lib 
file. I get the following error:


"module barlib is in file 'barlib.d' which cannot be read"

here is what I currently have:

the file to build the library
-
module barlib;
import std.stdio;

class BarLib
{
this(){}
void Talk(string msg)
{
writeln("BL: %s", msg);
}
}

the dub.json to build the lib
-
{
"name": "dbar",
"authors": ["moe"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "staticLibrary",
"configurations": [
{
"name": "debug",
"targetPath": "bin/debug",
"buildOptions": ["debugMode", "debugInfo"]
},
{
"name": "release",
"targetPath": "bin/release",
"buildOptions": ["releaseMode", "optimize", "inline"]
}
]
}


the file for the app

import barlib;
import std.stdio;

void main()
{
auto b = new BarLib();
b.Talk("Hello from barlib");
}

the dub.json for the app

{
"name": "dfoo",
"authors": ["moe"],
"description": "A minimal D application.",
"copyright": "Copyright © 2016, root",
"license": "proprietary",
"platforms": ["windows"],
"versions": ["DesktopApp"],
"targetType": "executable",
"configurations": [
{
"name": "debug",
"targetPath": "bin/debug",
"buildOptions": ["debugMode", "debugInfo"]
},
{
"name": "release",
"targetPath": "bin/release",
"buildOptions": ["releaseMode", "optimize", "inline"]
}
]
}

I can successfully build the barlib.lib file but I can not use it 
in the app. I thought that it should be possible to simply import 
the module from the lib-file, but the app cannot find the module. 
I would like to be able to build .lib and .dll files and use them 
in other applications. Can someone tell me what I am missing?