1. 程式人生 > 實用技巧 >VS Code配置連結庫檔案

VS Code配置連結庫檔案

配置VS code在C語言中呼叫gsl庫檔案
先確認gsl庫,gcc都已正確安裝,命令列
g++ -L/usr/local/lib hello.c -o hello -lgsl -lgslcblas -lm
沒有錯誤則可以配置VS code

tasks.json要配置args欄位
launch.json要配置environment欄位
c_cpp_properties.json要配置includePath欄位

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",
            }

c_cpp_properties.json

            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/gsl"
            ],

主檔案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;
}