我需要设置一种从make文件调试程序的方法。具体来说,当我输入MongoDB shell version: 3.0.6
connecting to: ds037824.mongolab.com:37824/mydatabase
2015-10-15T10:45:14.185+0530 W NETWORK Failed to connect to 54.198.170.179:37824 after 5000 milliseconds, giving up.
2015-10-15T10:45:14.188+0530 E QUERY Error: couldn't connect to server ds037824.mongolab.com:37824 (54.198.170.179), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
时,我需要程序正常运行。但是当这个标志不存在时,我需要在整个代码中运行一些make -B FLAG=-DNDEBUG
命令。
为了澄清我需要知道如何检查我的C代码中是否存在此标志,我认为它与assert()
有关,我只是不知道从哪里去。
原谅我的无知,任何回应都将不胜感激!
答案 0 :(得分:0)
使用或不使用“FLAG = -DNDEBUG”调用make时,在Makefile中需要这样的规则:
import java.util.Scanner;
public class PizzaOrders
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0;
double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0;
String custlastname = "", pizzasize = "", pizzatype ="", response = "";
/*The loop will prompt the user to see if the customer would like to order a pizza.
If yes, it will prompt the user for their last name, choice of pizza type, and
their choice of pizza size. The two possible responses are yes and no. The dummy
value for the loop is no. */
System.out.print("Do you want to order one pizza?" +
"Type yes or no (all lower case), then press the ENTER key.");
response = input.next();
while (!(response.equals("no")))
{
/*Validate the user's response. If it is valid, prompt for the required
values, perform the calculation, and output the result. If invalid,
output an error message. */
if (response.equals("yes"))
{
System.out.println("Please type in your last name (it can only be one word) and then press the ENTER key.");
custlastname = input.next();
System.out.println("Please type in your choice of pizza in all lower case letters. What you type into the keyboard must be" +
" pepperoni, veggie, cheese, or supreme.");
pizzatype = input.next();
System.out.println("Please type in your choice of pizza size in all lower case letters. What you type into the keyboard must" +
" be small, medium, or large.");
pizzasize = input.next();
/* Validate the entries for the calculation. The sizes must be small, medium, or large. The types of
pizza must be pepperoni, veggie, cheese, or supreme. For division, we must make sure the denominator
is not zero. If invalid, output an error message. */
if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") ||
pizzatype.equals("supreme") && pizzasize.equals("small") || pizzasize.equals("medium") ||
pizzasize.equals("large") && numberoforders !=0.0)
{
if (pizzatype.equals ("pepperoni"))
{
if (pizzasize.equals ("small"))
{
smallpizzas = smallpizzas + 1;
pizzacost = 8.50;
totalordercost = totalordercost + 8.50;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("medium"))
{
mediumpizzas = mediumpizzas + 1;
pizzacost = 9.50;
totalordercost = totalordercost + 9.50;
numberoforders = numberoforders + 1;
}
else if(pizzasize.equals ("large"))
{
largepizzas = largepizzas + 1;
pizzacost = 10.50;
totalordercost = totalordercost + 10.50;
numberoforders = numberoforders + 1;
}
}
else if (pizzatype.equals ("veggie"))
{
if (pizzasize.equals ("small"))
{
smallpizzas = smallpizzas + 1;
pizzacost = 10.00;
totalordercost = totalordercost + 10.00;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("medium"))
{
mediumpizzas = mediumpizzas + 1;
pizzacost = 12.25;
totalordercost = totalordercost + 12.25;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("large"))
{
largepizzas = largepizzas + 1;
pizzacost = 14.50;
totalordercost = totalordercost + 14.50;
numberoforders = numberoforders + 1;
}
}
else if (pizzatype.equals ("cheese"))
{
if (pizzasize.equals ("small"))
{
smallpizzas = smallpizzas + 1;
pizzacost = 7.00;
totalordercost = totalordercost + 7.00;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("medium"))
{
mediumpizzas = mediumpizzas + 1;
pizzacost = 8.00;
totalordercost = totalordercost + 8.00;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("large"))
{
largepizzas = largepizzas + 1;
pizzacost = 9.00;
totalordercost = totalordercost + 9.00;
numberoforders = numberoforders + 1;
}
}
else if (pizzatype.equals ("supreme"))
{
if (pizzasize.equals ("small"))
{
smallpizzas = smallpizzas + 1;
pizzacost = 11.00;
totalordercost = totalordercost + 11.00;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("medium"))
{
mediumpizzas = mediumpizzas + 1;
pizzacost = 14.00;
totalordercost = totalordercost + 14.00;
numberoforders = numberoforders + 1;
}
else if (pizzasize.equals ("large"))
{
largepizzas = largepizzas + 1;
pizzacost = 16.00;
totalordercost = totalordercost + 16.00;
numberoforders = numberoforders + 1;
}
averagecost = totalordercost/(double)numberoforders;
}
}
System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost);
}
else
System.out.println("What you have typed in is incorrect. Your response must be yes or no.");
System.out.println("Do you want to order one pizza? Type yes or no" +
"(all lower case), then press the ENTER key.");
response = input.next();
}
}
}
在你的C代码中你需要这样的东西:
%.o: %.c
gcc -c $(FLAG) $<
答案 1 :(得分:0)
假设您正在讨论标准库中的assert
宏(#define
中的<assert.h>
d),那么您不必做任何事情。该库已经处理了NDEBUG
标志。
如果您想让自己的代码只在宏不是#define
时才能执行操作,请使用您在问题中已经怀疑的#ifdef
。
例如,我们可能有一个条件过于复杂而无法放入单个assert
表达式中,因此我们需要一个变量。但如果assert
扩展为空,那么我们就不希望计算出该值。所以我们可能会使用这样的东西。
int
questionable(const int * numbers, size_t length)
{
#ifndef NDEBUG
/* Assert that the numbers are not all the same. */
int min = INT_MAX;
int max = INT_MIN;
size_t i;
for (i = 0; i < length; ++i)
{
if (numbers[i] < min)
min = numbers[i];
if (numbers[i] > max)
max = numbers[i];
}
assert(length >= 2);
assert(max > min);
#endif
/* Now do what you're supposed to do with the numbers... */
return 0;
}
请注意,这种编码风格会导致难以阅读的代码,并且要求 Heisenbugs这些代码非常难以调试。表达这一点的更好方法是使用函数。
/* 1st helper function */
static int
minimum(const int * numbers, size_t length)
{
int min = INT_MAX;
size_t i;
for (i = 0; i < length; ++i)
{
if (numbers[i] < min)
min = numbers[i];
}
return min;
}
/* 2nd helper function */
static int
maximum(const int * numbers, size_t length)
{
int max = INT_MIN;
size_t i;
for (i = 0; i < length; ++i)
{
if (numbers[i] > max)
max = numbers[i];
}
return max;
}
/* your actual function */
int
better(const int * numbers, int length)
{
/* no nasty `#ifdef`s */
assert(length >= 2);
assert(minimum(numbers, length) < maximum(numbers, length));
/* Now do what you're supposed to do with the numbers... */
return 0;
}