我有一个正在用 C++ 开发的项目。构建文件是使用 GN 和 Clang 创建的。但问题是我无法从 GN 创建可执行的 main。您可以查看下面定义的函数以供参考。
添加.cpp
#include "pch.h"
#include "framework.h"
#include "add.h"
#include<iostream>
void A::add1(int a,int b)
{
std::cout<<"The value of A is "<<a<<" and the value of B is"<<b<<" and thier sum is"<<a+b<<std::endl;
}
子文件
#include "pch.h"
#include "framework.h"
#include "sub.h"
#include<iostream>
void check::sub1(int a,int b)
{
std::cout<<"A-B is "<<a-b<<std::endl;
}
同样,乘法函数和除法函数。
pch 函数定义为:-
#ifndef PCH_H
#define PCH_H
#define BUILDING_DLL_LIB
#include "framework.h"
#endif
框架功能:-
#if defined(_MSC_VER) // Microsoft compiler
#include <windows.h>
#elif defined(__GNUC__) // GNU compiler
#include <dlfcn.h>
#endif
主要的:-
#include<iostream>
#include "../add/SampleDll.h"
#include "../add/add.h"
#include "../sub/sub.h"
#include "../mul/mul.h"
#include "../div/div.h"
int main()
{
CSampleDll obj;
A obj1;
check obj2;
C obj3;
D obj4;
obj1.add1(10,5);
obj2.sub1(10,5);
obj3.mul1(10,5);
obj4.div1(10,5);
}
但问题是在使用 gn 构建文件时,我在 linux 上收到此错误:-
clang++ -pthread -rdynamic -fopenmp -Wl,-rpath-link= -Wl,--no-undefined -Wl,--strip-discarded -Wl,--gc-sections -m64 -o ./main @main.rsp
/usr/bin/ld: obj/main/main.o: in function `main':
/mnt/d/test/dll_so/out/../main/main.cpp:9: undefined reference to `CSampleDll::CSampleDll()'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:16: undefined reference to `A::add1(int, int)'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:17: undefined reference to `check::sub1(int, int)'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:18: undefined reference to `C::mul1(int, int)'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:19: undefined reference to `D::div1(int, int)'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:20: undefined reference to `CSampleDll::~CSampleDll()'
/usr/bin/ld: /mnt/d/test/dll_so/out/../main/main.cpp:20: undefined reference to `CSampleDll::~CSampleDll()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
正在为每个函数正确创建共享库,但是在链接时,我收到了上述错误。为 main 执行的命令是:-
clang++ -g -Wl,/debug -Wl,/ignore:4099 -Wl,/dynamicbase -Wl,/subsystem:console -Wl,/nxcompat -Wl,/safeseh:no -Wl,/nodefaultlib:libcmt.lib -Wl,/incremental -Wl,/machine:x64 -m64 -o ./main.exe @main.exe.rsp ./add.lib ./sub.lib ./mul.lib ./div.lib -lmsvcrt.lib -lgdi32.lib -lshell32.lib -ladvapi32.lib -ldelayimp.lib -lBcrypt.lib -loldnames.lib
LIBRARY_PATH
另外,我尝试在 Window Subsytem for linux (WSL) 中使用以下方法设置路径:-
LIBRARY_PATH=/mnt/path/to/lib
export LIBRARY_PATH
当上述方法LIBRARY_PATH
不起作用时,我尝试LD_LIBRARY_PATH
使用:-
LD_LIBRARY_PATH=/mnt/path/to/lib::$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/mnt/path/to/lib:$LD_LIBRARY_PATH
没有什么对 WSL 有用,我很困惑我错在哪里。请,任何帮助将不胜感激。
PS- 上面的项目正在 Windows 上运行。