我非常清楚在linux环境下使用g ++编译C++程序.但是,可能是我错过了一些东西,我得到了这种奇怪的输出/行为.
我在test.cpp中有源文件.为了编译这个,我做到了
(1) g++ -c test.cpp g++ -o test test.o ./test
一切正常.但是,当我在同一阶段进行编译和链接时,就像这样
(2) g++ test.cpp -o test ./test => Works fine (3) g++ -c test.cpp -o test => Doesn't work
在我的最后一种情况下,测试生成但不再可执行; 但在我的猜测它应该工作正常.那么,有什么不对或我需要更改一些设置/配置?
我正在使用g ++ 4.3.3
谢谢.
任何人都可以解释这是如何工作的
#define maxMacro(a,b) ( (a) > (b) ) ? (a) : (b)
inline int maxInline(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int i = 1; j = 2, k = 0;
k = maxMacro(i,j++); // now i = 1, j = 4 and k = 3, Why ?? Where is it incremented ?
//reset values
i = 1; j = 2, k = 0;
k = maxInline(i,j++); // now i …Run Code Online (Sandbox Code Playgroud) 这里的所有构造函数方法都做同样的事情.我主要使用method2但今天第一次看到method3.在某些地方看过method1但不知道它们之间的确切差异是什么?哪一个是定义构造函数的最佳方法,为什么?是否涉及任何性能问题?
1 class Test
2 {
3 private:
4 int a;
5 char *b;
6 public:
7 Test(){};
8
9 // method 1
10 Test(int &vara, char *& varb) : a(vara), b(varb){}
11
12 // method 2
13 Test(int &vara, char *& varb)
14 {
15 a = vara;
16 b = varb;
17 }
18
19 //method 3
20 Test(int &vara, char *& varb)
21 {
22 this->a = vara;
23 this->b = varb;
24 }
25
26 ~Test(){} …Run Code Online (Sandbox Code Playgroud) 我喜欢vin中的cindent,smartindent和=%features,它们可以正确缩进代码.但是,我有一个问题,它用8个空格缩进所有内容,如果我有很少的嵌套ifs,它可以是非常长的行,就像这里,虽然在第一个地方有这么多嵌套ifs是另一个问题.
4 int main()
5 {
6 if(x)
7 {
8 if(u)
9 {
10 if(y)
11 {
12 }
13 }
14 }
15 }
Run Code Online (Sandbox Code Playgroud)
我试图设置ts = 1但仍然无法正常工作.
有没有办法在使用这些功能时将默认缩进级别设置为4个空格?
编辑设置sw = 4解决了问题.难怪vim总是让我感到惊讶:)
我有一个小程序,可以从服务器和客户端发送和接收数据,反之亦然.一切正常,但我看不到双方收到的消息,它总是0字节.它没有给出任何编译错误但是没有按照我想要的方式工作.你能不能看看我做错了什么?谢谢
//客户
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char* argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
char* hostname = "localhost";
char* server = "localhost";
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(hostname, "5000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 0;
}
// loop through all the results and …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译大型C++项目,我收到了这个奇怪的错误.我知道这是链接错误但无法弄清楚究竟是什么.
test_oqlquery.o:(.rodata._ZTV8r_MarrayIhE[vtable for r_Marray]+0x8): undefined reference to r_Marray<unsigned char>::~r_Marray()'
test_oqlquery.o:(.rodata._ZTV8r_MarrayIhE[vtable for r_Marray<unsigned char>]+0xc): undefined reference tor_Marray::~r_Marray()'
test_oqlquery.o:(.rodata._ZTV8r_MarrayIhE[vtable for r_Marray]+0x28): undefined reference to `r_Marray::print_status(std::basic_ostream >&) const'
这个错误是什么意思 ?并且,是否可以看到发生错误的行号?怎么样 ?我主要关心的是这意味着什么
".rodata._ZTV8r_MarrayIhE[vtable for r_Marray]+0x28"
实际上,我的错误是这样的,但不知道为什么角括号内的所有东西都丢失了,所以用""替换它们,这里是详细的错误,它与模板实例化有关,以及
test_oqlquery.o:(.rodata._ZTV8r_MarrayIhE[vtable for r_Marray"unsigned char"]+0x8): undefined reference to `r_Marray"unsigned char"::~r_Marray()'
我正在使用g ++ 4.3.3.
请原谅,我不能在这里提交完整的源代码,因为它非常大并跨越多个目录.
非常感谢.
我正在寻找一本好书,参考材料,涉及类的前向声明,特别是。当源位于多个目录中时,例如。dirA 中的 A 类在 dirB 中的 B 类中向前声明?这是怎么做到的?
另外,有任何有关模板问题、高级用途和实例化问题的材料吗?
谢谢。
我必须经常编译小程序并运行它.因为 g++ -W -Wall file.cpp -o out每次为每个cpp文件编写编译命令都很繁琐,我编写了一个小脚本来编译.
这是我写的脚本
#!/bin/bash
g++ -W -Wall $1 -o $1.out
Run Code Online (Sandbox Code Playgroud)
所以,如果我必须编译file.cpp,我会这样做compile file.cpp ,它将file.cpp.out为我创建可执行文件.并且,这样的file.cpp没有任何头文件或任何其他依赖项.
我知道如何为特定的file.cpp编写makefile(非常简单的情况)
file: file.cpp
g++ -W -Wall file.cpp -o file
Run Code Online (Sandbox Code Playgroud)
但如果我必须编译file2.cpp,我必须再次更改上面的makefile或写新的.所以,我想要做的是,当我发出make file.cpp命令时,它会将文件生成为可执行文件.当我给 make file2.cpp它时,它会生成file2作为可执行文件,同样适用于其他cpp文件.
我的班级结构如下
add.h有类add,它有方法int add(int,int),add.cpp包含add.h和定义方法add
sub.h有class sub,它有方法int sub(int,int),sub.cpp包含sub.h并定义方法sub
现在,main.h有class main,它包含add.h和sub.h; main类还有一些打印方法来打印结果; main.cpp使用方法add和sub来做计算
我的问题是,类main和class add之间的关系是什么类main和class sub.Main.h只包含add.h和sub.h,因此这个关系有任何名称.AFAIK它不是继承,它不是组合或聚合.