<div class="gmail_quote">On Mon, Oct 8, 2012 at 10:40 PM, James Bigler <span dir="ltr"><<a href="mailto:jamesbigler@gmail.com" target="_blank">jamesbigler@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

In my project I need to manually link against a special version of libstdc++, so I manually set the target link language to C and then add my special library to the link line.  On Linux this seems to work just fine, but on OSX it still add -lstdc++ to the link like.  Here's my cmake code:<div>


<br></div><div><div>cmake_minimum_required(VERSION 2.8)</div><div>project(linking_fun)</div><div><br></div><div>add_library(a SHARED a.cpp)</div><div>set_target_properties(a PROPERTIES LINKER_LANGUAGE "C")</div>


<div><br></div><div>add_library(b SHARED b.c)</div></div><div><br></div><div>a.cpp and b.c both simply have 'int foo() { return 3;}'</div><div><br></div><div><br></div><div>And here's the compiler output which has the -lstdc++ on the link line for library a, but not for library b.</div>


<div><br></div><div><div><font color="#ff0000">Linking C shared library liba.dylib</font></div><div>/opt/local/bin/cmake -E cmake_link_script CMakeFiles/a.dir/link.txt --verbose=1</div><div>/usr/bin/gcc   -dynamiclib -Wl,-headerpad_max_install_names   -o liba.dylib -install_name /Users/jbigler/code/temp/cmake/static-libstdcpp/liba.dylib CMakeFiles/a.dir/a.cpp.o <span style="background-color:rgb(255,255,0)">-lstdc++</span> /usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a </div>


<div>/opt/local/bin/cmake -E cmake_progress_report /Users/jbigler/code/temp/cmake/static-libstdcpp/CMakeFiles  1</div><div>[ 50%] Built target a</div><div>make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/depend</div>

<div>
cd /Users/jbigler/code/temp/cmake/static-libstdcpp && /opt/local/bin/cmake -E cmake_depends "Unix Makefiles" /Users/jbigler/code/temp/cmake/static-libstdcpp /Users/jbigler/code/temp/cmake/static-libstdcpp /Users/jbigler/code/temp/cmake/static-libstdcpp /Users/jbigler/code/temp/cmake/static-libstdcpp /Users/jbigler/code/temp/cmake/static-libstdcpp/CMakeFiles/b.dir/DependInfo.cmake --color=</div>


<div>make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/build</div><div><font color="#ff0000">Linking C shared library libb.dylib</font></div><div>/opt/local/bin/cmake -E cmake_link_script CMakeFiles/b.dir/link.txt --verbose=1</div>


<div>/usr/bin/gcc   -dynamiclib -Wl,-headerpad_max_install_names   -o libb.dylib -install_name /Users/jbigler/code/temp/cmake/static-libstdcpp/libb.dylib CMakeFiles/b.dir/b.c.o </div></div><div><br></div><div>Is this a bug or a "feature"?</div>

<span class="HOEnZb"><font color="#888888">
<div><br></div><div>James</div>
</font></span></blockquote></div><br><div>I spent some time in the debugger to try and understand why CMake is making the decisions it is.  It comes down to this function which loops over all the LinkClosures and calls AddImplicitLinkInfo for all languages that *don't* match the language of the target.</div>

<div><br></div><div><div>//----------------------------------------------------------------------------</div><div>void cmComputeLinkInformation::AddImplicitLinkInfo()</div><div>{</div><div>  // The link closure lists all languages whose implicit info is needed.</div>

<div>  cmTarget::LinkClosure const* lc=this->Target->GetLinkClosure(this->Config);</div><div>  for(std::vector<std::string>::const_iterator li = lc->Languages.begin();</div><div>      li != lc->Languages.end(); ++li)</div>

<div>    {</div><div>    // Skip those of the linker language.  They are implicit.</div><div>    if(*li != this->LinkLanguage)</div><div>      {</div><div>      this->AddImplicitLinkInfo(*li);</div><div>      }</div>

<div>    }</div><div>}</div><div><br></div></div><div><br></div><div>The LinkClosure is computed and the only language in it is CXX due to the CXX-ness of the input sources.  I'm not sure what to think about this.  I told CMake to link the target as if it were a C target, then it decides that it needs CXX libraries because it found CXX sources in my library.  </div>

<div><br></div><div>If I go through the trouble of specifying that I want to link the library as if it were C, why do the source files get to override my property?  </div><div><br></div><div>It also seems to me that the source file language should only guide the link language when the user didn't specify it explicitly.</div>

<div><br></div><div>James</div>