Haskell之Yesod开发–边踩坑边开发(4.1)

来源:互联网 发布:飞利浦医疗知乎 编辑:程序博客网 时间:2024/06/08 07:42

这里写链接内容

下面的文字仅仅是备份

使用 minghc 在 windows 开发 yesod 项目的笔记

系统环境
Windows 8 64bit English
备注(winxp32位经过测试不成功,请勿尝试)

安装开发环境

安装各种软件以尽量减少对系统的影响为原则。例如不修改系统环境变量、注册表之类。

minghc
下载运行安装程序

安装完成

基本使用

打开 windows 的命令窗口,运行以下命令

d:\minghc-7.8.4-x86_64\switch\minghc-7.8

这是个 bat,会设置好 PATH 环境变量。

然后就可以在这个命令行窗口开始工作了。

cabal update
如果全新安装,或很久没做过 cabal update,那就现在做一次。

运气不好的话,这一步有时候很慢,可以用环境变量指定 proxy

安装 yesod 相关工具

安装 yesod-bin

找个临时目录,执行
cabal get yesod-bin

d:\tmp>cabal get yesod-bin
Downloading yesod-bin-1.4.11…
Unpacking to yesod-bin-1.4.11\

进入已解包的目录,执行例行的 cabal sandbox install 步骤

cabal sandbox init
cabal install –dependencies-only
cabal install

install 之后,会在 .cabal-sandbox\bin 目录下生成多个 exe 文件,只需把这些文件 copy 至方便的目录即可。考虑到 minghc 已设置好的 PATH ,大概放在 minghc 安装目录下的 bin 目录是个好的选择。

最后,丢弃这个目录 d:\tmp\yesod-bin-1.4.11,不要再在这个目录下工作了

构建一个最简单的使用 MySQL 的 Yesod 项目

因为用 cabal sandbox,故以下过程,基本上可以理解为每次新项目都要做一次(除非与sandbox无关的操作)。

安装 MySQL Connector/C

这就是一个C库。与 sandbox 无关,做一次就可以了。

下载页面
http://dev.mysql.com/downloads/connector/c/

下载连接
http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-6.1.6-winx64.zip

找个合适的目录解开即可。

生成代码
如果是新的项目,应使用 yesod.exe 生成模板代码。
在命令行窗口内,找个合适的目录,执行 yesod init 按提示操作。
下面红色字是我的输入,其它是命令的提示文字。

d:\tmp>yesod init
Welcome to the Yesod scaffolder.
I’m going to be creating a skeleton Yesod project for you.

What do you want to call your project? We’ll use this for the cabal name.

Project name: mysql-site
Yesod uses Persistent for its (you guessed it) persistence layer.
This tool will build in either SQLite or PostgreSQL or MongoDB support for you.
We recommend starting with SQLite: it has no dependencies.

s      = sqlitep      = postgresqlpf     = postgresql + Fay (experimental)mongo  = mongodbmysql  = MySQLsimple = no database, no authmini   = bare bones, the "Hello World" of multi-file Yesod apps         (Note: not configured to work with yesod devel)url    = Let me specify URL containing a site (advanced)

So, what’ll it be? mysql
That’s it! I’m creating your files now…


                 ___                        {-)   |\                   [m,].-"-.   /  [][__][__]         \(/\__/\)/  [__][__][__][__]~~~~  |  |  [][__][__][__][__][] /   |  [__][__][__][__][__]| /| |  [][__][__][__][__][]| || |  ~~~~

ejm [][][][][],_, _/


The foundation for your web application has been built.

There are a lot of resources to help you use Yesod.
Start with the book: http://www.yesodweb.com/book
Take part in the community: http://yesodweb.com/page/community

It’s highly recommended to follow the quick start guide for
installing Yesod: http://www.yesodweb.com/page/quickstart

If your system is already configured correctly, please run:

cd mysql-site && cabal install -j –enable-tests –max-backjumps=-1 –reorder
-goals && yesod devel

安装依赖的库

然后进入新建出来的 mysql-site 目录。(这目录名就是你输入的项目名,但你可以生成之后随便改。)做一次 cabal sandbox init

d:\tmp\mysql-site>cabal sandbox init
Writing a default package environment file to
d:\tmp\mysql-site\cabal.sandbox.config
Creating a new sandbox at D:\tmp\mysql-site.cabal-sandbox

然后又是 cabal install –dependencies-only。这过程也会有点长,因为会安装许多库。而且会以失败结束,因为 mysql 相关的 haskell 库安装会失败,但大部分库还是会成功安装到 sandbox 去的。(如果生成代码时选择的是 postgresql ,也会出类似的错误。选择 sqlite 则应该可以直接完全安装成功。)

如果看到的不是跟数据库相关的库安装也失败了,看看是不是暂时性的网络错误引起的。比如,我测试时就见过 getAddrInfo 错误,这看上去就是 DNS 解释错误而已。
这类问题的解决方法就是多执行几次 cabal install –dependencies-only

最后,不能安装的 haskell 库应该是以下几个:
pcre-light
mysql-simple
mysql
persistent-mysql

这些库都是直接或间接因为依赖 MySQL 引起的。这里真正有问题的库只是其中两个
pcre-light
mysql

下面试试能不能解决这几个问题

———————– 以下未成功---------
pcre-light 的安装
先要解决 pcre-light
pcre-light 安装不了原因是找不到 pcre 的头文件和C库文件。
解决方法就两个思路:找现成的,或直接从源代码build出来。

找现成
按千人提供的stackoverflow 答案做

从源代码 build
这方法以前做过,记得是可以的。但现在有现成的,就不自己build了。

mysql 的安装

注意:mysql 库用下面的方法能 build 成功,开发用是可以的,但产品用就太危险了,因为下面修改去掉了部分线程、signal 相关的操作,实际使用会有什么影响并不了解。

我们要修改代码,因此先把代码取出来。

d:\tmp\mysql-site>cd ..

d:\tmp>cabal get mysql
Unpacking to mysql-0.1.1.8\

d:\tmp>cd mysql-0.1.1.8

d:\tmp\mysql-0.1.1.8>cabal sandbox init –sandbox=..\mysql-site.cabal-sandbox
Writing a default package environment file to
d:\tmp\mysql-0.1.1.8\cabal.sandbox.config
Using an existing sandbox located at D:\tmp\mysql-site.cabal-sandbox

注意:我在 init sandbox 时指定了前面生成的 sandbox 的路径。因为最终我们是要把这个库安装到前面那个 sandbox 去的。这是多个相关项目共享一个 cabal sandbox 的方法。

现在如果执行 cabal install 错误提示应该是这样的
d:\tmp\mysql-0.1.1.8>cabal install
Resolving dependencies…
[1 of 1] Compiling Main ( dist\dist-sandbox-835f98c1\setup\setup.hs, dist\dist-sandbox-835f98c1\setup\Main.o )
Linking .\dist/dist-sandbox-835f98c1\setup\setup.exe …
Configuring mysql-0.1.1.8…
setup.exe: The program ‘mysql_config’ is required but it could not be found
Failed to install mysql-0.1.1.8
cabal: Error: some packages failed to install:
mysql-0.1.1.8 failed during the configure step. The exception was:
ExitFailure 1

对代码作如下修改

文件 Database/MySQL/Base/C.hsc: 增加
#include <windows.h> 所有 foreign import ccall .. ,把 ccall 改成 stdcall。
另一个 hsc 文件也要增加 windows.h。
cbits/mysql_signals.c: 去掉所有跟 pthread 有关的函数调用:就是让两个 macro 变成空操作,init_rts_sigset函数整个去掉。
最后是 Setup.lhs
把以下两行
include <- mysqlConfig [“–include”]
libs <- mysqlConfig [“–libs”]
改成:
let include = [ “-IC:\Progra~2\MySQL\MySQLC~1.1\include” ]
let libs = [ “-LC:\Progra~2\MySQL\MySQLC~1.1\lib”, “-llibmysql”]
当然,具体路径看 connector 所在目录而定。

现在 cabal install 就可以成功安装了。

现在回到之前生成的项目mysql-site的目录,再执行一次
cabal install –dependencies-only
这次应该完全没错误了

现在就可以执行
yesod devel

0 0
原创粉丝点击