Haskell 安装 Cairo

2023-11-29 03:20
文章标签 安装 haskell cairo

本文主要是介绍Haskell 安装 Cairo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景

Haskell 项目需要使用到柱状图,折线图等(demo 代码)

步骤(默认已安装 stack, cabal, ghcup)

nameversion
stack2.11.1
cabal3.8.1.0
ghcup0.1.20.0
  1. 在 package.yaml 中添加所需依赖 Chart 和 Chart-cairo
name:                a
version:             0.1.0.0
github:              "githubuser/a"
license:             BSD-3-Clause
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2023 Author name here"extra-source-files:
- README.md
- CHANGELOG.md# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/a#readme>dependencies:
- base >= 4.7 && < 5ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraintslibrary:source-dirs: - src- appdependencies: - bytestring- http-conduit- sqlite-simple- Chart- text# - Chart-diagrams# - diagrams# - diagrams-svg# - diagrams-lib- colour- data-default-class# common- Chart-cairo- lens- aeson- stm- split- text- time- binary- binary-orphans- old-locale- directory- pretty-simple- string-conversionsexecutables:a-exe:main:                Main.hssource-dirs:         appghc-options:- -threaded- -rtsopts- -with-rtsopts=-Ndependencies:- a- sqlite-simple- Chart- text- bytestring# - Chart-diagrams# - diagrams# - diagrams-svg# - diagrams-lib- colour- data-default-class# common- Chart-cairo- lens- aeson- stm- split- text- time- binary- binary-orphans- old-locale- directory- pretty-simple- string-conversionstests:a-test:main:                Spec.hssource-dirs:         testghc-options:- -threaded- -rtsopts- -with-rtsopts=-Ndependencies:- a

运行 stack-build,编译失败,大概提示未指定 cairo 版本。

  1. 在 stack.yaml 中添加 chart 版本信息
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
# resolver: lts-21.13
resolver: lts-21.13# url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/22.yaml# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
#   subdirs:
#   - auto-update
#   - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
#   commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []# Override default flag values for local packages and extra-deps
# flags: {}# Extra package databases containing global packages
# extra-package-dbs: []# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of Stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.11"
#
# Override the architecture used by Stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by Stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
extra-deps:
- Chart-1.9.5
- Chart-cairo-1.9.4.1flags: {}extra-package-dbs: []

运行 stack build,报错。

ErrorTrace:

cairo      > configure
cairo      > [1 of 3] Compiling Main             ( C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\Setup.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\Main.o )
cairo      > [2 of 3] Compiling StackSetupShim   ( C:\sr\setup-exe-src\setup-shim-9p6GVs8J.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\StackSetupShim.o )
cairo      > [3 of 3] Linking C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup.exe
cairo      > Configuring cairo-0.13.10.0...
cairo      > Error: setup.EXE: The pkg-config package 'cairo' version >=1.2.0 is required
cairo      > but it could not be found.
cairo      >
Progress 1/3Error: [S-7282]Stack failed to execute the build plan.While executing the build plan, Stack encountered the error:[S-7011]While building package cairo-0.13.10.0 (scroll up to its section to see the error) using:C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup --verbose=1 --builddir=.stack-work\dist\22605e11 configure --with-ghc=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-9.4.7.exe --with-ghc-pkg=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-pkg-9.4.7.exe --user --package-db=clear --package-db=global --package-db=C:\sr\snapshots\9095fbbf\pkgdb --libdir=C:\sr\snapshots\9095fbbf\lib --bindir=C:\sr\snapshots\9095fbbf\bin --datadir=C:\sr\snapshots\9095fbbf\share --libexecdir=C:\sr\snapshots\9095fbbf\libexec --sysconfdir=C:\sr\snapshots\9095fbbf\etc --docdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --htmldir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --haddockdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --dependency=Cabal=Cabal-3.8.1.0 --dependency=array=array-0.5.4.0 --dependency=base=base-4.17.2.0 --dependency=bytestring=bytestring-0.11.5.2 --dependency=gtk2hs-buildtools=gtk2hs-buildtools-0.13.10.0-7OxGn4htize3gcv93EYR9n --dependency=mtl=mtl-2.2.2 --dependency=text=text-2.0.2 --dependency=utf8-string=utf8-string-1.0.2-KqazO8nUWrDMJ2shK7HoQ -fcairo_pdf -fcairo_ps -fcairo_svg --ghc-options -haddock --extra-include-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\include --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\bin --exact-configuration --ghc-option=-fhide-source-pathsProcess exited with code: ExitFailure 1

提示 gcc 自带的 pkg-config 没有高于 1.2.0 的 cairo 包

  1. 安装 MSYS2, 并配置 Path, 目的是用到 MSYS2 中的 pkg-config.
    在这里插入图片描述
    安装相关工具
# gcc
pacman -S mingw-w64-ucrt-x86_64-gcc# gtk
pacman -S mingw-w64-x86_64-gtk4# 语言开发包
pacman -S mingw-w64-x86_64-toolchain base-devel
  1. 重启 PowerShell 和 VSCode
  2. 在 VSCode 的 PowerShell 中运行
$Env:PATH -split ';'PS C:\Users\df366\a\a> $Env:PATH -split ';'
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Program Files\dotnet\
C:\Program Files\Docker\Docker\resources\bin
D:\Git\cmd
D:\MSYS2\mingw64\bin
D:\MSYS2\mingw64\lib
D:\MSYS2\mingw64\include
C:\Users\df366\AppData\Roaming\local\bin
C:\Users\df366\AppData\Local\Microsoft\WindowsApps
C:\Users\df366\.dotnet\tools
C:\Program Files\MongoDB\Server\7.0\bin
D:\Microsoft VS Code\bin
C:\ghcup\bin
D:\PyCharm 2023.2.2\bin

注意:这里必须重启 VSCode,否则系统环境中的变量不会更新。

  1. 检查 pkg-config
Get-Command pkg-configCommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     pkg-config.exe                                     0.0.0.0    D:\MSYS2\mingw64\bin\pkg-config.exe

发现 pkg-config 已经变成 MSYS2 下的了

  1. 重新执行 stack build,运行成功
PS C:\Users\df366\a\a> stack build
a-0.1.0.0: unregistering (local file changes: app\Main.hs)
a> build (lib + exe)
Preprocessing library for a-0.1.0.0..
Building library for a-0.1.0.0..
[6 of 7] Compiling Main [Source file changed]app\Main.hs:3:1: warning: [-Wunused-imports]The import of 鈥楩etch鈥?is redundantexcept perhaps to import instances from 鈥楩etch鈥?[0mTo import instances alone, use: import Fetch()|
3 | import Fetch| ^^^^^^^^^^^^app\Main.hs:4:1: warning: [-Wunused-imports]The import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
4 | import Data.Text (Text, splitOn)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:5:1: warning: [-Wunused-imports]The qualified import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
5 | import qualified Data.Text as T| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:6:1: warning: [-Wunused-imports]The import of 鈥楧atabase鈥?is redundantexcept perhaps to import instances from 鈥楧atabase鈥?[0mTo import instances alone, use: import Database()|
6 | import Database| ^^^^^^^^^^^^^^^
[7 of 7] Linking app\Main.exe [Objects changed]
Preprocessing executable 'a-exe' for a-0.1.0.0..
Building executable 'a-exe' for a-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]app\Main.hs:3:1: warning: [-Wunused-imports]The import of 鈥楩etch鈥?is redundantexcept perhaps to import instances from 鈥楩etch鈥?[0mTo import instances alone, use: import Fetch()|
3 | import Fetch| ^^^^^^^^^^^^app\Main.hs:4:1: warning: [-Wunused-imports]The import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
4 | import Data.Text (Text, splitOn)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:5:1: warning: [-Wunused-imports]The qualified import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
5 | import qualified Data.Text as T| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:6:1: warning: [-Wunused-imports]The import of 鈥楧atabase鈥?is redundantexcept perhaps to import instances from 鈥楧atabase鈥?[0mTo import instances alone, use: import Database()|
6 | import Database| ^^^^^^^^^^^^^^^
[3 of 3] Linking .stack-work\dist\22605e11\build\a-exe\a-exe.exe [Objects changed]
a> copy/register
Installing library in C:\Users\df366\a\a\.stack-work\install\1b692ffa\lib\x86_64-windows-ghc-9.4.7\a-0.1.0.0-5LEhwaAmmakDiuPZZmv67T
Installing executable a-exe in C:\Users\df366\a\a\.stack-work\install\1b692ffa\bin
Registering library for a-0.1.0.0..
  1. Main 代码
module Main (main) whereimport Fetch
import Data.Text (Text, splitOn)
import qualified Data.Text as T
import Database
-- import Diagrams.Prelude
-- import Diagrams.Backend.SVG
-- import Diagrams.Attributes
-- import Diagrams.Backend.SVG.CmdLineimport Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Colour
import Control.Lens
import Data.Default.Classchart :: Bool -> Renderable ()
chart borders = toRenderable layoutwherelayout =layout_title .~ "Sample Bars" ++ btitle$ layout_title_style . font_size .~ 10$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels$ layout_y_axis . laxis_override .~ axisGridHide$ layout_left_axis_visibility . axis_show_ticks .~ False$ layout_plots .~ [ plotBars bars2 ]$ def :: Layout PlotIndex Doublebars2 = plot_bars_titles .~ ["Cash","Equity"]$ plot_bars_values .~ addIndexes [[20,45],[45,30],[30,20],[70,25]]$ plot_bars_style .~ BarsClustered$ plot_bars_spacing .~ BarsFixGap 30 5$ plot_bars_item_styles .~ map mkstyle (cycle defaultColorSeq)$ defalabels = [ "Jun", "Jul", "Aug", "Sep", "Oct" ]btitle = if borders then "" else " (no borders)"bstyle = if borders then Just (solidLine 1.0 $ opaque black) else Nothingmkstyle c = (solidFillStyle c, bstyle)main :: IO ()
main = do_ <- renderableToFile def "example11_big.png" (chart True)return ()-- spilitByLine :: String -> [String]
-- spilitByLine s = map T.unpack $ splitOn (T.pack "\n") (T.pack s)-- main :: IO ()
-- main = do
--     a <- downloadURL "http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-2022.csv"--     let items = spilitByLine a
--     print $ length items
--     print $ head items
--     print $ items !! 1
--     print 33
--     print "3.6.2.0"
  1. 输出图
    在这里插入图片描述

  2. cabal 文件(无需修改,在 package.yaml 添加依赖后,运行 stack update 会同步更新)

cabal-version: 2.2-- This file has been generated from package.yaml by hpack version 0.35.2.
--
-- see: https://github.com/sol/hpackname:           a
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/githubuser/a#readme>
homepage:       https://github.com/githubuser/a#readme
bug-reports:    https://github.com/githubuser/a/issues
author:         Author name here
maintainer:     example@example.com
copyright:      2023 Author name here
license:        BSD-3-Clause
license-file:   LICENSE
build-type:     Simple
extra-source-files:README.mdCHANGELOG.mdsource-repository headtype: gitlocation: https://github.com/githubuser/alibraryexposed-modules:DatabaseFetchLibTypesMainother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:srcappghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraintsbuild-depends:Chart, Chart-cairo, aeson, base >=4.7 && <5, binary, binary-orphans, bytestring, colour, data-default-class, directory, http-conduit, lens, old-locale, pretty-simple, split, sqlite-simple, stm, string-conversions, text, timedefault-language: Haskell2010executable a-exemain-is: Main.hsother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:appghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-Nbuild-depends:Chart, Chart-cairo, a, aeson, base >=4.7 && <5, binary, binary-orphans, bytestring, colour, data-default-class, directory, lens, old-locale, pretty-simple, split, sqlite-simple, stm, string-conversions, text, timedefault-language: Haskell2010test-suite a-testtype: exitcode-stdio-1.0main-is: Spec.hsother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:testghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-Nbuild-depends:a, base >=4.7 && <5default-language: Haskell2010

参考博客:
https://stackoverflow.com/questions/57940965/pkg-config-installed-but-not-found
https://stackoverflow.com/questions/44314060/haskell-stack-nix-build-fails-requiring-pkg-config-0-9-0-and-cairo-1
https://stackoverflow.com/questions/5336865/cant-install-cairo-with-cabal-on-windows-how-to-get-pkg-config-on-win
https://stackoverflow.com/questions/63396432/having-dependency-issues-while-trying-to-install-threadscope-with-stack-the-iss
https://github.com/commercialhaskell/stack/issues/377
https://stackoverflow.com/questions/77441659/haskell-after-install-the-conduit-package
https://zhuanlan.zhihu.com/p/623146940

这篇关于Haskell 安装 Cairo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/431255

相关文章

Jsoncpp的安装与使用方式

《Jsoncpp的安装与使用方式》JsonCpp是一个用于解析和生成JSON数据的C++库,它支持解析JSON文件或字符串到C++对象,以及将C++对象序列化回JSON格式,安装JsonCpp可以通过... 目录安装jsoncppJsoncpp的使用Value类构造函数检测保存的数据类型提取数据对json数

mac安装redis全过程

《mac安装redis全过程》文章内容主要介绍了如何从官网下载指定版本的Redis,以及如何在自定义目录下安装和启动Redis,还提到了如何修改Redis的密码和配置文件,以及使用RedisInsig... 目录MAC安装Redis安装启动redis 配置redis 常用命令总结mac安装redis官网下

如何安装 Ubuntu 24.04 LTS 桌面版或服务器? Ubuntu安装指南

《如何安装Ubuntu24.04LTS桌面版或服务器?Ubuntu安装指南》对于我们程序员来说,有一个好用的操作系统、好的编程环境也是很重要,如何安装Ubuntu24.04LTS桌面... Ubuntu 24.04 LTS,代号 Noble NumBAT,于 2024 年 4 月 25 日正式发布,引入了众

如何安装HWE内核? Ubuntu安装hwe内核解决硬件太新的问题

《如何安装HWE内核?Ubuntu安装hwe内核解决硬件太新的问题》今天的主角就是hwe内核(hardwareenablementkernel),一般安装的Ubuntu都是初始内核,不能很好地支... 对于追求系统稳定性,又想充分利用最新硬件特性的 Ubuntu 用户来说,HWEXBQgUbdlna(Har

python中poetry安装依赖

《python中poetry安装依赖》本文主要介绍了Poetry工具及其在Python项目中的安装和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前言1. 为什么pip install poetry 会造成依赖冲突1.1 全局环境依赖混淆:1

windows端python版本管理工具pyenv-win安装使用

《windows端python版本管理工具pyenv-win安装使用》:本文主要介绍如何通过git方式下载和配置pyenv-win,包括下载、克隆仓库、配置环境变量等步骤,同时还详细介绍了如何使用... 目录pyenv-win 下载配置环境变量使用 pyenv-win 管理 python 版本一、安装 和

Linux下MySQL8.0.26安装教程

《Linux下MySQL8.0.26安装教程》文章详细介绍了如何在Linux系统上安装和配置MySQL,包括下载、解压、安装依赖、启动服务、获取默认密码、设置密码、支持远程登录以及创建表,感兴趣的朋友... 目录1.找到官网下载位置1.访问mysql存档2.下载社区版3.百度网盘中2.linux安装配置1.

Kibana的安装和配置全过程

《Kibana的安装和配置全过程》Kibana是一个开源的数据分析和可视化平台,它与Elasticsearch紧密集成,提供了一个直观的Web界面,使您可以快速地搜索、分析和可视化数据,在本文中,我们... 目录Kibana的安装和配置1.安装Java运行环境2.下载Kibana3.解压缩Kibana4.配

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7