CMake linking issue with SFML/GLEW in Linux

Started by
10 comments, last by hatfarm 10 years, 7 months ago

Okay, I decided I'd make a new post since the last one kinda got away from the original intent. So, I'm trying to build my application and I've got all my code compiling, but it's not linking and I'm not sure why. I kinda reworked the setup, I've got my engine that I've worked on and it compiles and links fine. My game code is compiling, but it will not find the link to the GLEW and SFML libraries. I cannot figure out why. It works fine with my setup in Windows, but Linux apparently hates it. Here's my top level CMakeLists.txt:


cmake_minimum_required(VERSION 2.6.4)
project(Balls)

ADD_SUBDIRECTORY(bate)

include_directories(
	${CMAKE_CURRENT_SOURCE_DIR}/include
	${BATE_INCLUDES}
)

set(EXEC_NAME "Balls to the Walls")

set(Balls_SRCS	
	src/application.cpp	
	src/ball.cpp
	src/ballswindow.cpp
	src/brick.cpp
	src/component.cpp
	src/entity.cpp	
	src/main.cpp
	src/menustate.cpp
	src/position.cpp
	src/system.cpp
	src/velocity.cpp
	
)

set(Balls_INC
	include/application.h
	include/ball.h
	include/ballswindow.h
	include/brick.h
	include/component.h
	include/entity.h
	include/menustate.h
	include/position.h
	include/system.h
	include/velocity.h
)

ADD_DEFINITIONS( -DSFML_STATIC )
	
add_executable(${EXEC_NAME} ${Balls_SRCS} ${Balls_INC})

SET_TARGET_PROPERTIES(${EXEC_NAME}
	PROPERTIES
	COMPILE_DEFINITIONS
	"GLEW_STATIC="
)

TARGET_LINK_LIBRARIES(${EXEC_NAME} BatE )

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    # Linux specific code
    SET_TARGET_PROPERTIES(${EXEC_NAME}
	PROPERTIES
	COMPILE_DEFINITIONS
	"IS_LINUX="
    )
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

# Install target
install(TARGETS ${EXEC_NAME} DESTINATION bin)

BatE is my engine (Battle Engine). I've tried linking SFML here, but that didn't change anything. Any advice?

Advertisement

Gah! I figured out why the problem is happening, a bit. Part of the reason is that the environment variables are not getting read properly by CMake. I've got SFML_ROOT defined in the /etc/environment file, and then I also can print out $SFML_ROOT in the terminal and it works, but CMake does not recognize them (I put a MESSAGE statement in there). So, that's the problem, but I'm not sure how to fix it. I even wrote a script that created the environment variable, then exported it and prints it out, it prints them all out, but it is not finding it inside of CMake...

Anyone have any ideas?

Well, I got around the issue a bit. I created a bash script that exports the variables and then calls cmake-gui. It works, it's lame, but it works. However, then it has trouble finding any of the libraries that SFML links against. This is incredibly frustrating.

Sorry for the delay in replying. Been running around lately..

In order to work with environment variables in CMake, you simply prepend "ENV" to the variable. So, in order to tell Cmake to read your environment variable SFML_ROOT, you do the following:

SET( SFML_ROOT ENV{SFML_ROOT} )

Now SFML_ROOT has been imported from the environment variable and you can use it as a normal CMake variable. That should get around the script file issue if I understood your needs correctly.

That's actually how I've been working with it. I'm still not sure why that issue is happening, but now I'm having issues with the libraries linked by SFML. I'm working on that, but I'm having an issue with CMake saying: "Cannot specify link libraries for target

"/usr/lib/i386-linux-gnu/libGLU.so" which is not built by this project."

Any ideas? It appears I've got GLU installed, that doesn't appear to be the issue. Why is it so much more brutal in Linux?

That's actually how I've been working with it. I'm still not sure why that issue is happening, but now I'm having issues with the libraries linked by SFML. I'm working on that, but I'm having an issue with CMake saying: "Cannot specify link libraries for target

"/usr/lib/i386-linux-gnu/libGLU.so" which is not built by this project."

Any ideas? It appears I've got GLU installed, that doesn't appear to be the issue. Why is it so much more brutal in Linux?

This is the annoying thing in this thread (and the old one), I usually find Linux to be very simple to fix compared to all the f'd up Windows nonsense. I don't mean to say the thread is annoying or anything, just that the worst case for me is always Windows and you always seem to run into Linux issues. :)

To be honest, it really looks like a problem in the CMake code but without further details I simply can not give you a lot of feedback. I "THINK" that the error is specifying that you are trying to set target dependencies on something which is not part of your build, i.e. libGLU. Unfortunately I don't see anything wrong with your posted CMake lists file but it doesn't have anything to do with libGLU so, that doesn't mean much.

Given the information I'd step back and remove that bash script and try to fix that bit first. There is a problem somewhere and by using a script you are not fixing a possible source of the problems. You should not need a script at all, that's the reason for CMake. :)

So, I've fixed a couple of issues. This OpenGL issue has gone away, I was telling it to link against my executable and not the library, which was a problem, because the executable wasn't defined in the library's .txt file. The script issue has just gone away, it wasn't solved by just restarting, because I didn't change anything between restarts in order to get it to work, but it's working without the script, so I'm not complaining about not making any headway. I'm having a problem with it not finding X11 in Linux. I have X11, because it compiled SFML fine, but as far as my makefile is concerned, it's not being linked properly. I get this error when I try to make in Linux:


/usr/bin/ld: /home/hatfarm/SFML-2.1//lib/libsfml-window-s.a(WindowImplX11.cpp.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/bin/ld: note: 'XGetWindowAttributes' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

Here's the code I have in my CMakeLists.txt file that should ensure that X11 is linked properly:


IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    find_package(X11 REQUIRED)
    set(LIB_INCLUDES
        ${LIB_INCLUDES}
        ${X11_INCLUDE_DIR})
    set(LIB_LINKS
    	${LIB_LINKS} ${X11_LIBRARIES} )
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

That's in both my library and executable's CMakeFiles.txt...

I know that LIB_INCLUDES and LIB_LINKS are correctly used because it doesn't complain about OpenGL. Any ideas?

That's a pretty confusing one. It looks as if the compiler "is" attempting to link the library, i.e. the "Invalid operation" message but the symbols are not getting read in correctly. It makes me wonder if somehow you have mixed 32bit/64bit code somehow. I setup a quick little experiment myself and had no problem getting SFML and X11 linked into a little test app, so that didn't give me any leads. I'll keep thinking about it but I don't see anything particularly wrong. The one thing I could suggest is to simply insert a "MESSAGE( "LIB_LINKS: ${LIB_LINKS}" )" after your block of code and make sure it looks correct, doesn't have any extra spaces, incorrect backslashes etc. That's always my starting point, just start spitting out as much as possible till one looks incorrect. :)

Well, the weirdest thing is, I can make the examples for SFML fine. There's specifically an X11 example, and I checked that CMake file in order to get what I should put here. When I make that, it works fine. I really can't figure out what the deal is here though... I actually have the exact message print that you put, and nothing looks funny, except the include directories don't appear to have ';' after them, but that doesn't appear to be the issue. This is incredibly frustrating, because I just want to code.

Cross platform stuff is a pain in the bloody ass unfortunately. Cmake makes it considerably easier but is still not a silver bullet. If you can't get it figured out soon, you could give me source access and I'll fix things up for you. I'll even sign an NDA or whatever if needed. NOTE: This is a one time offer, just figure since you've used my new forum the most, it's worth it.. smile.png

This topic is closed to new replies.

Advertisement