在汇编代码生成,可用库,语言功能等方面编译C和C++代码时,3个编译器CC,gcc,g ++有什么区别?
gcc和g ++之间的链接过程有什么不同吗?
我有一个很大的C项目,我只是将部分代码切换到C++.代码尚未使用std C++库,因此-llibstdc++现在不需要.
另一个C/C++集成问题:我正在尝试libcl.a使用我在C++库中的功能(让我们称之为)更新一些遗留C库(让我们称之为libcppl.a).该liblc.a库正在我的环境中使用,并使用GCC(在C编译器模式下)链接到许多C项目中:
>> gcc prog.c -lcl
Run Code Online (Sandbox Code Playgroud)
在libcl.a目前包括的cl.o对象文件(具有创建gcc从cl.c+ cl.h).
在libcppl.a由的cppl.o对象文件(具有创建g++从cppl.cpp+ cppl.h).
因为现有的应用程序是用C语言编写的,而构建脚本是使用GCC的,所以我希望尽可能简单地过渡到更新的库.因此,我想继续使用GCC作为主编译器,但仍然能够与更新的库链接.
找到这个答案,我可以使用以下方法将C++对象链接到GCC C项目-lstdc++:
>> gcc -c cl.c -o cl.o
>> g++ -c cppl.c -o cppl.o
>> ar rcs libcl.a cl.o cppl.o
>> gcc prog.c -lcl -lstdc++
Run Code Online (Sandbox Code Playgroud)
但是,我想消除 libstdc++ 在编译命令行中明确提到的内容.
我试图做的是在cl库中包含libstdc ++,方法是:
>> ar rcs libcl.a cl.o cppl.o /usr/lib/libstdc++.so.6
Run Code Online (Sandbox Code Playgroud)
但是,在构建应用程序时,我得到:
>> …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++编译一个简单的Hello World程序但是我一直收到以下错误......为什么?
gcc -o HelloWorldCompiled HelloWorld.cc
/tmp/ccvLW1ei.o: In function `main':
HelloWorld.cc:(.text+0xa): undefined reference to `std::cout'
HelloWorld.cc:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccvLW1ei.o: In function `__static_initialization_and_destruction_0(int, int)':
HelloWorld.cc:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
HelloWorld.cc:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccvLW1ei.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这是我的计划:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
}
Run Code Online (Sandbox Code Playgroud) 在下面的程序中,我尝试将结构传递给函数.但我得到错误,我不明白为什么.我在这个程序中犯了什么错误?
我gcc用来编译这个c程序.
#include <stdio.h>
struct tester {
int x;
int *ptr;
};
void function(tester t);
int main() {
tester t;
t.x = 10;
t.ptr = & t.x;
function(t);
}
void function(tester t) {
printf("%d\n%p\n",t.x,t.ptr);
}
Run Code Online (Sandbox Code Playgroud)
错误:
gcc tester.c -o tester
tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in …Run Code Online (Sandbox Code Playgroud) 在gcc中获取编译错误,即使它在Visual Studio C++上正常工作
#include <iostream>
using std::cout; // tried it, didn't work
using std::cin; // tried it, didn't work
using std::endl; // tried it, didn't work
using namespace std;
Run Code Online (Sandbox Code Playgroud)
这是错误,我在尝试编译它时得到了.
hw2_part2.cpp:(.text+0xa2): undefined reference to `std::cout'
hw2_part2.cpp:(.text+0xa7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
hw2_part2.cpp:(.text+0xb6): undefined reference to `std::cout'
hw2_part2.cpp:(.text+0xbb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
hw2_part2.cpp:(.text+0xca): undefined reference to `std::cout'
hw2_part2.cpp:(.text+0xcf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& …Run Code Online (Sandbox Code Playgroud)