本文总结了使用第三方库函数时将其路径告诉编译器(gcc和g++同理)的2种常用方式,并举例说明了每种方式的具体用法。
方法一:在编译自己的项目时添加-L和-I编译选项
1)添加头文件路径:
-I #指明头文件的路径
2)添加库文件路径:
-L #指定目录。link的时候,去找的目录。gcc会先从-L指定的目录去找,然后才查找默认路径。(告诉gcc,-l库名最可能在这个目录下)。
-l #指定文件(库名),linking options
注:-l紧接着就是库名,这里的库名不是真正的库文件名。比如说数学库,它的库名是m,他的库文件名是libm.so。再比如说matlab eigen库,它的库名是eng,它的库文件名是libeng.so。很容易总结得:把库文件名的头lib和尾.so去掉就是库名了。在使用时,“-leng”就告诉gcc在链接阶段引用共享函数库libeng.so。
方法二:将库路径添加到环境变量
1)添加头文件路径:
在/etc/profile中添加(根据语言不同,任选其一):
export C_INCLUDE_PATH=C_INCLUDE_PATH:头文件路径 #c
export CPLUS_INCLUDE_PATH=CPLUS_INCLUDE_PATH:头文件路径 #c++
export OBJC_INCLUDE_PATH=OBJC_INCLUDE_PATH:头文件路径 #java
终端重启后需执行一次source。
另有一种方法:在/etc/ld.so.conf文件中加入自定义的lib库的路径,然后执行sudo /sbin/ldconfig,这个方法对所有终端有效。
2)添加库文件路径:
LIBRARY_PATH #used by gcc before compilation to search for directories containing libraries that need to be linked to your program.
LD_LIBRARY_PATH #used by your program to search for directories containing the libraries after it has been successfully compiled and linked.
例如:
MATLAB=/opt/MATLAB/R2012a
export LIBRARY_PATH=$LIBRARY_PATH:$MATLAB/bin/glnxa64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATLAB/bin/glnxa64
题外话,顺便提一下LIBRARY_PATH和LD_LIBRARY_PATH的区别:
我们知道Linux下有2种库:static libraries和shared libraries。如(这里)阐述的,静态库是在编译期间会被链接并拷贝到你的程序中,因此运行时不再需要该静态库。动态库在编译时并不会被拷贝到你的程序中,而是在程序运行时才被载入,因此在程序运行时还需要动态库存在,这时就会用到LD_LIBRARY_PATH指定的路径去查找这个动态库。The libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.
举例说明以上2种方法的具体用法:
假设随便一个简单的函数叫做matlab_eigen.cpp,它用到第三方库(matlab eigen函数),内容如下:
/*
* matlab_eigen.cpp
*
* This is an example of how to create a surface contour plot in MATLAB
* http://cn.mathworks.com/matlabcentral/fileexchange/35311-matlab-plot-gallery-surface-contour-plot/content/html/Surface_Contour_Plot.html
*/
#include <iostream>
#include <math.h>
#include "engine.h"
int main() {
Engine *ep; //定义Matlab引擎指针。
if (!(ep=engOpen("\0"))) //测试是否启动Matlab引擎成功。
{
std::cout<< "Can't start MATLAB engine"<<std::endl;
return EXIT_FAILURE;
}
// 向matlab发送命令。在matlab内部自己去产生即将绘制的曲线上的数据。
// Create a grid of x and y data
engEvalString(ep, "y = -10:0.5:10;x = -10:0.5:10; [X, Y] = meshgrid(x, y);");
// Create the function values for Z = f(X,Y)
engEvalString(ep, "Z = sin(sqrt(X.^2+Y.^2)) ./ sqrt(X.^2+Y.^2);");
// Create a surface contour plor using the surfc function, Adjust the view angle
engEvalString(ep, "figure;surfc(X, Y, Z); view(-38, 18);");
// Add title and axis labels
engEvalString(ep, "title('Normal Response');xlabel('x');ylabel('y');zlabel('z')");
//Use cin.get() to make sure that we pause long enough to be able to see the plot.
std::cout<<"Hit any key to exit!"<<std::endl;
std::cin.get();
return EXIT_SUCCESS;
}
用方法一编译:
$ g++ matlab_eigen.cpp -o matlab_eigen -I/opt/MATLAB/R2012a/extern/include -L/opt/MATLAB/R2012a/bin/glnxa64 -leng -lmx
用方法二编译:
设置环境变量:
MATLAB=/opt/MATLAB/R2012a
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATLAB/bin/glnxa64
export LIBRARY_PATH=$LIBRARY_PATH:$MATLAB/bin/glnxa64
export CPLUS_INCLUDE_PATH=CPLUS_INCLUDE_PATH:$MATLAB/extern/include
编译:
$ g++ matlab_eigen.cpp -o matlab_eigen -leng -lmx
运行及结果:(如果你编译过程遇到错误可以参考我这篇文章。)
./matlab_eigen
Hit any key to exit!
参考:
http://walkerqt.blog.51cto.com/1310630/1300119
http://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path
http://blog.chinaunix.net/uid-26980210-id-3365027.html
---------------------
作者:arackethis
来源:CSDN
原文:https://blog.csdn.net/arackethis/article/details/43342655
自己下载源码啊手动编译gcc/g++
引用: https://blog.csdn.net/l919898756/article/details/81015617
1. 编译新版gcc需要旧版支持
1)确认是否有安装gcc
gcc -v
2) 如果没有安装gcc, 如果机子有联网,则可以使用以下方法
center-os系统:
yum -y install gcc
yum -y install gcc-g++
ubuntu系统:
sudo apt-get installgcc
sudo apt-get installg++
2 确认有旧版的gcc之后,下载gcc_4.9.3
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-4.9.3/gcc-4.9.3.tar.bz2
1)解压
tar -jxvf gcc-4.9.3.tar.bz2
2)下载依赖项
先进入到解压后的目录
cd gcc-4.9.3
可有如下2中方法下载依赖项(2.1和2.2任选其一)。
2.1)用其自带的下载脚本
./contrib/download_prerequisites
下载好以后会得到3个目录gmp、mpc、mpfr
2.2)如果上面脚本连接失败,则可以到国内csdn下载(当然不仅限于此)
http://download.csdn.net/detail/alex_my/7681299
下载好以后会得到3个压缩包,手动解压并建立软链接
tar -jxvf gmp-4.3.2.tar.bz2
tar -zxvf mpc-0.8.1.tar.gz
tar -jxvf mpfr-2.4.2.tar.bz2
ln -sf gmp-4.3.2 gmp
ln -sf mpc-0.8.1 mpc
ln -sf mpfr-2.4.2 mpfr
3)编译前配置configure
../gcc-4.9.3/configure --prefix=/home/lh/gcc-4.9.3/build--enable-threads=posix --disable-checking --enable--long-long--with-system-zlib --enable-languages=c,c++
../gcc-4.9.3/configure --prefix=/home/lh/gcc-4.9.3/build--enable-threads=posix --disable-checking --enable--long-long--with-system-zlib --enable-languages=c,c++
4)可视机器cpu核心数调整,加参数-j。
make –j64
假如编译错误,可能需要安装sudo apt-get install bison build-essential flex
5)经过漫长的等待后
sudo make install
6) 修改系统使用的gcc/g++
一般gcc/g++会安装到/usr/local/bin中,而编译时默认使用的是/usr/bin中的版本,因此,需要重新制作链接。
sudo rm -rf /usr/bin/gcc
sudo rm -rf /usr/bin/g++
sudo ln -s /usr/local/bin/gcc/usr/bin/gcc
sudo ln -s /usr/local/bin/g++/usr/bin/g++
7) 最后,检验安装是否成功,使用/home/lh/gcc-4.9.3/build/bin/gcc-v
查看版本。会看到:
xxx@ubuntu:~/Develop/gcc-4.9.0$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.9.3/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ./configure --enable-checking=release --enable-languages=c,c++--disable-multilib
Thread model: posix
gcc version 4.9.3 20150325 (prerelease) (GCC)
修改默认gcc的路径到新版本
:exportPATH="/home/lh/gcc-4.9.3/build/bin/:$PATH"
(只对终端有效)
修改永久有效:
vim .bashrc
https://blog.csdn.net/l919898756/article/details/81015617
[转载]Compiling Binutils error: ‘TARGET_ALIAS’ undeclared
http://blog.sciencenet.cn/blog-365816-1013728.html
1) run configure as normal
2) run 'make' it will fail in the gas directory
3) run 'mv bfd/config.h bfd/config.h.bak' this forces the compilation to select the correct config.h
4) run 'cd gas/'
5) run 'make'
6) run 'cd ..'
7) run 'make' this will rebuild bfd, but it will skip over gas and avoid the error... however it will then descend into binutils and run into the same error...
8) run 'mv bfd/config.h bfd/config.h.bak'
9) run 'cd binutils/'
10) run 'make'
11) run 'cd ..'
12) run 'make' this will rebuild bfd, but it will skip over gas and binutils... this time everything else compiles without error
编译安装高版本glibc:
glibc 安装位置configure --prefix 不要直接指定到 /usr 或者默认,否则可能导致系统损坏。
我操作时,遇到configure错误是: as,ld 版本低了,需安装高版本的binutils
binutils在Make时也遇到错误:子目录make时 -I路径导致config.h查找首先找到另一个子目录bfd,而忽略当前子模块config.h的头文件,gcc -H 选项可打印出包含头文件顺序, 后面我的解决方法是:先在build目录make ,遇到宏未定义错误后,mv bfd/config.h bfd/config.h.bak, 然后进入宏未定义的各个子目录目录 make ,然后再回到build目录make,make install
binutils升级后,再来配置glibc即可成功,然后在build目录make,make遇到错误
编译错误
[install gcc6.1.0]error: Unable to find a suitable type for HOST_WIDE_INT
https://blog.csdn.net/baidu_25845567/article/details/89600988
解决方法
unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE
make install 遇到错误
relocation error: /lib64/libpthread.so.0: symbol __libc_dl_error_tsd, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference
出现错误的原因是 当前build目录 包含了 libc.so.6之类文件,解决方法是 unset LD_LIBRARY_PATH
然后在 make install
使用指定glibc编译程序
有时候我们需要测试不同的glibc的性能,此时可以使用如下命令来指定动态库加载器和glibc,
g++ test_log.c -Wl,--rpath=/usr/local/lib -Wl,--dynamic-linker=/usr/local/lib/ld-linux-x86-64.so.2
其中/usr/local/lib为glibc动态库的路径,linker为动态装载器
以下test_log.c代码为例
/*
* learn for syslog/openlog
*
*
* */
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
//check the argc,usage e.g. ./a.out filename
if(argc != 2){
perror("argc != 2");
exit(1);
}
openlog(argv[0],LOG_PID | LOG_NOWAIT | LOG_NDELAY,LOG_USER);
syslog(LOG_INFO,"%s","hello,world");
closelog();
}
使用上述命令编译后,可以用/usr/bin/ldd查看a.out依赖的动态库
[jefby@localhost work]$ /usr/bin/ldd a.out
linux-vdso.so.1 => (0x00007fff3d9ff000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003c67400000)
libm.so.6 => /usr/local/lib/libm.so.6 (0x00007fdf9bc5c000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003c67000000)
libc.so.6 => /usr/local/lib/libc.so.6 (0x00007fdf9b8b8000)
/usr/local/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x0000003c5a000000)
说明已经替换成功,运行
./a.out jefby
然后使用tail来查看是否成功写入系统日志中
sudo tail /var/log/messages
如下所示,可以看到已经成功写入:
[jefby@localhost work]$ ./a.out jefby
[jefby@localhost work]$ sudo tail /var/log/messages -n 1
Aug 22 14:30:02 localhost ./a.out[12965]: hello,world
---------------------
作者:jefbai
来源:CSDN
原文:https://blog.csdn.net/jefbai/article/details/47859335