配置VS Code链接外部gsl库文件

配置VS code在C语言中调用gsl库文件

gsl安装
sudo apt-get install libgsl0-dev

先确认gsl库,gcc都已正确安装,命令行
gcc -L/usr/local/lib hello.c -o hello -lgsl -lgslcblas -lm
没有错误则可以配置VS code

需要配置以下两处:
tasks.json要配置args字段
launch.json要配置environment字段

tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{ "type": "shell",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				      "-g",
				      "${file}",
            			      "-L/usr/local/lib",
	                       	      "-lgsl", "-lgslcblas", "-lm",
				      "-o",
                                      "${fileDirname}/${fileBasenameNoExtension}"
        			],

launch.json

    "cwd": "${workspaceFolder}",
            "environment": [
            {
                "name":  "LD_LIBRARY_PATH",
                "value": "/usr/local/lib",
            }

主文件hello.c编译链接成功

#include<stdio.h>
#include<gsl/gsl_sf_bessel.h>

int main()
{    
    double x1 = 5.0;
    double y1 = gsl_sf_bessel_J0(x1);
    printf("J0(%g) = %.18e\n", x1, y1);

    return 0;
}

测试环境: Ubuntu 18.04.4 LTS

posted @ 2020-10-31 19:46  daidau  阅读(1427)  评论(0编辑  收藏  举报