V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xiaottt
V2EX  ›  程序员

问一下关于 C/C++的头文件和库的问题

  •  
  •   xiaottt · 2018-11-26 10:53:59 +08:00 · 2274 次点击
    这是一个创建于 1949 天前的主题,其中的信息可能已经有所发展或是发生改变。

    系统默认有一套 boost 库,但是比较的老旧;然后我自己本地也编译了一套 boost 库,稍微新一些。
    新库和老库不兼容,但是有多套系统使用这个环境,所以新老都必须保留。

    请问有什么方法,让 g++编译的时候默认选择老库,但是通过某些方式可以优先选择新库的形式来编译呢?这里主要包括头文件和库文件,连接的话使用静态链接。

    13 条回复    2018-11-26 17:53:54 +08:00
    shylockhg
        1
    shylockhg  
       2018-11-26 10:55:23 +08:00
    没听懂
    shylockhg
        2
    shylockhg  
       2018-11-26 10:56:25 +08:00
    哦,可以改.a 的软连接。。
    bp0
        3
    bp0  
       2018-11-26 11:08:22 +08:00
    用来做交叉编译的 g++是有--sysroot 选项的,这个选项可以指定头文件和库文件的路径。

    我这边的 mingw 中的 g++也有类似选项,楼主可以自己--help 看一下自己的 g++是否支持。
    wutiantong
        4
    wutiantong  
       2018-11-26 11:09:15 +08:00
    无论是写 makefile 还是写 cmake 都能搞定这个的呀
    shylockhg
        5
    shylockhg  
       2018-11-26 11:14:14 +08:00
    也可以用-l:libname.version
    bp0
        6
    bp0  
       2018-11-26 11:29:02 +08:00
    补充一下,如果头文件相同,可以在编译的时候指定-L 选项。这个选项用来指定库文件的路径。
    xiaottt
        7
    xiaottt  
    OP
       2018-11-26 11:35:05 +08:00
    @bp0

    Directories named by -I are searched before the standard system include directories.


    ENVIRONMENT

    Note that you can also specify places to search using options such as -B, -I and -L. These take precedence over places specified using environment variables, which in turn take
    precedence over those specified by the configuration of GCC.


    看了一下文档,好像是有这么一说。
    我来试试看
    coderluan
        8
    coderluan  
       2018-11-26 11:39:11 +08:00
    正经的是写 makefile,不正经的话可以用 shell 和环境变量解决。
    zbcwilliam
        9
    zbcwilliam  
       2018-11-26 14:13:29 +08:00
    makefile
    zynlp
        10
    zynlp  
       2018-11-26 14:30:06 +08:00 via iPhone
    两个不同版本库,同时存在,要么放不同目录,要么改名字,
    如果头文件都不一样,代码里就用宏去控制 include 的文件
    make 的时候指定要链接的库

    ps:远离 boost
    aa514758835
        11
    aa514758835  
       2018-11-26 15:56:08 +08:00
    linux 环境变量。windows 的话 在 vs 里面设置
    xiaottt
        12
    xiaottt  
    OP
       2018-11-26 17:53:31 +08:00
    将目录和库路径包含在 CMake 的文件中,是会优先使用指定路径的头文件和库的,这里已经没问题了。


    @zynlp
    xiaottt
        13
    xiaottt  
    OP
       2018-11-26 17:53:54 +08:00
    @zynlp

    为啥要远离 boost ???
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1589 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:57 · PVG 00:57 · LAX 09:57 · JFK 12:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.