{"action":"created","issue":{"url":"https://api.github.com/repos/waruqi/xmake/issues/42","repository_url":"https://api.github.com/repos/waruqi/xmake","labels_url":"https://api.github.com/repos/waruqi/xmake/issues/42/labels{/name}","comments_url":"https://api.github.com/repos/waruqi/xmake/issues/42/comments","events_url":"https://api.github.com/repos/waruqi/xmake/issues/42/events","html_url":"https://github.com/waruqi/xmake/issues/42","id":186987856,"number":42,"title":"关于add_defines和add_defines_if_ok的区别","user":{"login":"xiren7","id":9202827,"avatar_url":"https://avatars.githubusercontent.com/u/9202827?v=3","gravatar_id":"","url":"https://api.github.com/users/xiren7","html_url":"https://github.com/xiren7","followers_url":"https://api.github.com/users/xiren7/followers","following_url":"https://api.github.com/users/xiren7/following{/other_user}","gists_url":"https://api.github.com/users/xiren7/gists{/gist_id}","starred_url":"https://api.github.com/users/xiren7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiren7/subscriptions","organizations_url":"https://api.github.com/users/xiren7/orgs","repos_url":"https://api.github.com/users/xiren7/repos","events_url":"https://api.github.com/users/xiren7/events{/privacy}","received_events_url":"https://api.github.com/users/xiren7/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-11-03T06:40:57Z","updated_at":"2016-11-03T12:30:22Z","closed_at":null,"body":"在option里面使用这个两个命令, 我感觉这两个命令是等效的.\r\n\r\n从代码上好像也是等效的? 是在core/tool/compiler.lua的中的_addflags_from_target函数中执行的?\r\n\r\n1.如果这两个命令真的是等效的, 是否只保留一个就行了? 同理add_undefines和add_undefines_if_ok也应该是一样的?\r\n\r\n2.另外关于add_defines_h_if_ok, 这个命令是在头文件里面定义宏, 然后你在include这个头文件. 那为什么不直接用add_defines定义了, 最后的结果不是一样的吗? 不太明白为什么要生成这个头文件的必要."},"comment":{"url":"https://api.github.com/repos/waruqi/xmake/issues/comments/258129449","html_url":"https://github.com/waruqi/xmake/issues/42#issuecomment-258129449","issue_url":"https://api.github.com/repos/waruqi/xmake/issues/42","id":258129449,"user":{"login":"waruqi","id":151335,"avatar_url":"https://avatars.githubusercontent.com/u/151335?v=3","gravatar_id":"","url":"https://api.github.com/users/waruqi","html_url":"https://github.com/waruqi","followers_url":"https://api.github.com/users/waruqi/followers","following_url":"https://api.github.com/users/waruqi/following{/other_user}","gists_url":"https://api.github.com/users/waruqi/gists{/gist_id}","starred_url":"https://api.github.com/users/waruqi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waruqi/subscriptions","organizations_url":"https://api.github.com/users/waruqi/orgs","repos_url":"https://api.github.com/users/waruqi/repos","events_url":"https://api.github.com/users/waruqi/events{/privacy}","received_events_url":"https://api.github.com/users/waruqi/received_events","type":"User","site_admin":false},"created_at":"2016-11-03T12:30:22Z","updated_at":"2016-11-03T12:30:22Z","body":"稍微有点区别，option如果在不进行检测，仅仅用于手动配置的情况下，这两个效果都是一样的，就是\r\n\r\n```\r\n如果option启用，那么添加宏定义\r\n```\r\n\r\n但是在option用于自动检测的情况下，这两个是有区别的\r\n\r\n* add_defines：option中所有不带_if_ok的设置，都是在option检测前就设置的，此处用于在检测前添加宏定义\r\n* add_defines_if_ok：_if_ok结尾的表示，在option检测通过后，需要添加的配置，此处用于option检测通过后，添加宏定义\r\n\r\n例如：\r\n\r\n```lua\r\noption(\"pcre2\")\r\n    \r\n    -- 如果检测通过，会在config.h中添加此此宏开关定义     \r\n    add_defines_h_if_ok(\"$(prefix)_PACKAGE_HAVE_PCRE2\")\r\n\r\n    -- 在检测前预先添加PCRE2_CODE_UNIT_WIDTH=8的宏定义，否则编译检测是通不过的\r\n    -- 检测通过后，也会追加此宏定义\r\n    add_defines(\"PCRE2_CODE_UNIT_WIDTH=8\")\r\n\r\n    -- 而此宏定义，只会在检测通过后添加，当然手动配置启用，或者默认启用的时候，也会添加此设置\r\n    add_defines_if_ok(\"XXXX\")\r\n\r\n    -- 在检测前，添加libpcre-8.a的链接库，检测的时候会去尝试链接它，链接不通过，选项检测失败\r\n    -- 检测通过后，add_options(\"pcre2\") 到target的时候，也会自动追加此链接\r\n    add_links(\"pcre2-8\")\r\n\r\n    -- 检测pcre2/pcre2.h的pcre2_compile接口\r\n    add_cincludes(\"pcre2/pcre2.h\")\r\n    add_cfuncs(\"pcre2_compile\")\r\n```\r\n\r\n总而言之，option里面不带if_ok的add_xxx接口，检测前设置，检测成功后也会设置\r\n而带if_ok的add_xxx接口，仅在检测成功（或者手动启用，默认启用）后被设置\r\n\r\n具体可以参考这篇文章：[http://tboox.org/cn/2016/08/07/custom-option/](http://tboox.org/cn/2016/08/07/custom-option/)\r\n\r\n而add_defines_h_if_ok用于在config.h中添加宏定义，一般用于代码中进行条件编译使用\r\n\r\n具体config.h的设置，见[set_config_h](https://github.com/waruqi/xmake/wiki/set_config_h)"}}
{"actor":{"display_login":"waruqi"}}