make install pnetcdf 后如何链接头文件

make install pnetcdf 后如何链接头文件

我尝试在安装 pnet cdf 后按照说明进行操作。运行以下命令后:

cd pnetcdf-1.11.2
./configure --prefix=$HOME/PnetCDF --with-mpi=/usr/bin --enable-netcdf4 --enable-shared --enable-thread-safe
make -j8
make install

PATH=$HOME/PnetCDF/bin:$PATH ; export PATH

我收到以下消息:

PnetCDF has been successfully installed under
|          /home/mmann/PnetCDF
|
|  * PnetCDF header files have been installed in
|          /home/mmann/PnetCDF/include
| # * PnetCDF library files have been installed in
| #         /home/mmann/PnetCDF/lib
|  #* PnetCDF utility programs have been installed in
|  #        /home/mmann/PnetCDF/bin
|  *# PnetCDF man pages have been installed in
|      #    /home/mmann/PnetCDF/share/man
|
|  To compile your PnetCDF programs, please add the following to the command
|  line, so the compiler can find the PnetCDF header files:
| #     -I/home/mmann/PnetCDF/include
|
|  Add the following line to link your program to PnetCDF library:
|      -L/home/mmann/PnetCDF/lib -lpnetcdf

作为一个新手,我不清楚如何添加这些链接。

我是否运行以下命令?

g++ -o sms ./src/sms.o -I/home/mmann/PnetCDF/include

g++ -o sms ./src/sms.o -L/home/mmann/PnetCDF/lib -lpnetcdf

答案1

尽管听起来有些迂腐,但你不需要链接头文件 - 你包括头文件,以及关联图书馆。

如果sms是一个由单个翻译单元组成的可执行程序,其源代码包含在文件中sms.cpp,那么您可以在单个命令中执行两个步骤(编译和链接)

g++ -o sms -I/home/mmann/PnetCDF/include ./src/sms.cpp -L/home/mmann/PnetCDF/lib -lpnetcdf

如果程序分为多个源文件,那么通常将构建分为单独的编译和链接阶段,例如

g++ -o sms.o -I/home/mmann/PnetCDF/include -c ./src/sms.cpp
g++ -o some.o -I/home/mmann/PnetCDF/include -c ./src/some.cpp
g++ -o other.o -I/home/mmann/PnetCDF/include -c ./src/sms.other

进而

g++ -o sms sms.o some.o other.o -L/home/mmann/PnetCDF/lib -lpnetcdf

这样做的主要优点是,如果您对其中一个源文件进行了更改,则不需要重新编译所有其他文件(只需重新编译更改的文件,然后重新链接可执行文件)。

相关内容